TextView
文字后EditText
或ContextMenu
所知道的那样,我的问题是:如何在没有ContextMenu
的情况下选择文字或禁用显示文字?
此代码有效,但导致无法选择文本
public class MyEditText extends AppCompatEditText {
private final Context mContext;
private int mPreviousCursorPosition;
public MyEditText(Context context) {
super(context);
this.mContext = context;
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
}
public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.mContext = context;
}
@Override
protected void onSelectionChanged(int selStart, int selEnd) {
CharSequence text = getText();
if (text != null) {
if (selStart != selEnd) {
setSelection(selStart, selEnd);
return;
}
}
mPreviousCursorPosition = selStart;
super.onSelectionChanged(selStart, selEnd);
}
}
答案 0 :(得分:0)
对于API级别11或更高级别,请执行以下操作:
edittext.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
//This will prevent opening the menu
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
进一步阅读: