JunZhang
5 years ago
committed by
GitHub
9 changed files with 119 additions and 77 deletions
@ -0,0 +1,112 @@ |
|||||
|
/* |
||||
|
* Copyright (c) 2020 Cobo |
||||
|
* |
||||
|
* This program is free software: you can redistribute it and/or modify |
||||
|
* it under the terms of the GNU General Public License as published by |
||||
|
* the Free Software Foundation, either version 3 of the License, or |
||||
|
* (at your option) any later version. |
||||
|
* |
||||
|
* This program is distributed in the hope that it will be useful, |
||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
* GNU General Public License for more details. |
||||
|
* |
||||
|
* You should have received a copy of the GNU General Public License |
||||
|
* in the file COPYING. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
*/ |
||||
|
package com.cobo.cold.ui.views; |
||||
|
|
||||
|
import android.annotation.SuppressLint; |
||||
|
import android.content.Context; |
||||
|
import android.util.AttributeSet; |
||||
|
import android.view.ActionMode; |
||||
|
import android.view.Menu; |
||||
|
import android.view.MenuItem; |
||||
|
import android.view.MotionEvent; |
||||
|
import android.widget.EditText; |
||||
|
import android.widget.TextView; |
||||
|
|
||||
|
import java.lang.reflect.Field; |
||||
|
|
||||
|
@SuppressLint("AppCompatCustomView") |
||||
|
public class MenuHidingEditText extends EditText { |
||||
|
private final Context mContext; |
||||
|
|
||||
|
public MenuHidingEditText(Context context) { |
||||
|
super(context); |
||||
|
this.mContext = context; |
||||
|
|
||||
|
blockContextMenu(); |
||||
|
} |
||||
|
|
||||
|
public MenuHidingEditText(Context context, AttributeSet attrs) { |
||||
|
super(context, attrs); |
||||
|
this.mContext = context; |
||||
|
|
||||
|
blockContextMenu(); |
||||
|
} |
||||
|
|
||||
|
public MenuHidingEditText(Context context, AttributeSet attrs, int defStyle) { |
||||
|
super(context, attrs, defStyle); |
||||
|
this.mContext = context; |
||||
|
|
||||
|
blockContextMenu(); |
||||
|
} |
||||
|
|
||||
|
private void blockContextMenu() { |
||||
|
this.setCustomSelectionActionModeCallback(new BlockedActionModeCallback()); |
||||
|
this.setLongClickable(false); |
||||
|
this.setOnTouchListener((v, event) -> { |
||||
|
MenuHidingEditText.this.clearFocus(); |
||||
|
return false; |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean onTouchEvent(MotionEvent event) { |
||||
|
if (event.getAction() == MotionEvent.ACTION_DOWN) { |
||||
|
// setInsertionDisabled when user touches the view
|
||||
|
this.setInsertionDisabled(); |
||||
|
} |
||||
|
return super.onTouchEvent(event); |
||||
|
} |
||||
|
|
||||
|
private void setInsertionDisabled() { |
||||
|
try { |
||||
|
Field editorField = TextView.class.getDeclaredField("mEditor"); |
||||
|
editorField.setAccessible(true); |
||||
|
Object editorObject = editorField.get(this); |
||||
|
|
||||
|
Class editorClass = Class.forName("android.widget.Editor"); |
||||
|
Field mInsertionControllerEnabledField = editorClass.getDeclaredField("mInsertionControllerEnabled"); |
||||
|
mInsertionControllerEnabledField.setAccessible(true); |
||||
|
mInsertionControllerEnabledField.set(editorObject, false); |
||||
|
} |
||||
|
catch (Exception ignored) { |
||||
|
// ignore exception here
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean isSuggestionsEnabled() { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
private class BlockedActionModeCallback implements ActionMode.Callback { |
||||
|
|
||||
|
public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
public void onDestroyActionMode(ActionMode mode) { |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue