问题
If I create a simple app with single Activity that contains a single EditText, and I do
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);
then this prevents the keyboard from appearing (in my tests on Android 5.0 and 7.1). This is what I want, as requested in these questions:
- Disable soft-keyboard from EditText but still allow copy/paste?
- How to disable Android Soft Keyboard for a particular activity?
- Android: Disable soft keyboard at all EditTexts
- How to disable keypad popup when on edittext?
- Disable keyboard on EditText
The source code is
public void setTextIsSelectable(boolean selectable) {
if (!selectable && mEditor == null) return; // false is default value with no edit data
createEditorIfNeeded();
if (mEditor.mTextIsSelectable == selectable) return;
mEditor.mTextIsSelectable = selectable;
setFocusableInTouchMode(selectable);
setFocusable(selectable);
setClickable(selectable);
setLongClickable(selectable);
// mInputType should already be EditorInfo.TYPE_NULL and mInput should be null
setMovementMethod(selectable ? ArrowKeyMovementMethod.getInstance() : null);
setText(mText, selectable ? BufferType.SPANNABLE : BufferType.NORMAL);
// Called by setText above, but safer in case of future code changes
mEditor.prepareCursorControllers();
}
But I don't see what about this disables the Input Method from appearing.
Calling the following methods all return true
whether I set setTextIsSelectable
or not.
editText.isFocusable(); // true
editText.isFocusableInTouchMode(); // true
editText.isClickable(); // true
editText.isLongClickable(); // true
I'm partly asking because I'm curious, but also because I need to disable the system keyboard in my app. I want to understand what is happening so that I can be sure that it is really doing what I think it is doing.
Update
Follow-up question and answer:
- How to enable keyboard on touch after disabling it with setTextIsSelectable
回答1:
The keyboard isn't shown when you call TextView.setTextIsSelectable(true)
and then select text as a result of Editor.startSelectionActionModeInternal, which checks if TextView.isTextSelectable
is false
before showing the text selection ActionMode
.
final boolean selectionStarted = mTextActionMode != null;
if (selectionStarted && !mTextView.isTextSelectable() && mShowSoftInputOnFocus) {
// Show the IME to be able to replace text, except when selecting non editable text.
final InputMethodManager imm = InputMethodManager.peekInstance();
if (imm != null) {
imm.showSoftInput(mTextView, 0, null);
}
}
My guess to why it's implemented this way is probably because the framework team decided they wanted text Editor
could predict to be selectable to not jump around the screen as a result of the keyboard moving it around. It was probably just a UI decision. According to git it's been that way since 2012, but the commit doesn't mention anything specifically regarding that implementation.
来源:https://stackoverflow.com/questions/45227805/how-does-settextisselectable-prevent-the-keyboard-from-appearing