setTextIsSelectable 如何防止键盘出现?
How does setTextIsSelectable prevent the keyboard from appearing?
如果我使用包含单个 EditText 的单个 Activity 创建一个简单的应用程序,并且我这样做
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);
然后这会阻止键盘出现(在我对 Android 5.0 和 7.1 的测试中)。这就是我想要的,正如这些问题中所要求的:
- 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
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();
}
但我不明白这会导致输入法不显示。
下面的方法全部调用return true
不管我是否设置setTextIsSelectable
editText.isFocusable(); // true
editText.isFocusableInTouchMode(); // true
editText.isClickable(); // true
editText.isLongClickable(); // true
我问这个问题,一方面是因为我很好奇,另一方面是因为我需要在我的应用程序中禁用系统键盘。我想了解正在发生的事情,以便我可以确定它确实在按照我的想法行事。
更新
后续问答:
- How to enable keyboard on touch after disabling it with setTextIsSelectable
当您调用 TextView.setTextIsSelectable(true)
然后调用 select 文本作为 Editor.startSelectionActionModeInternal
的结果时键盘不显示,它检查 TextView.isTextSelectable
是否为 false
在显示文本 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);
}
}
我猜为什么要这样实现可能是因为框架团队决定他们希望文本 Editor
可以预测 select 不会因为键盘而在屏幕上跳来跳去移动它。这可能只是一个 UI 的决定。据 git 称,自 2012 年以来一直如此,但该提交并未具体提及有关该实施的任何内容。
如果我使用包含单个 EditText 的单个 Activity 创建一个简单的应用程序,并且我这样做
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);
然后这会阻止键盘出现(在我对 Android 5.0 和 7.1 的测试中)。这就是我想要的,正如这些问题中所要求的:
- 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
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();
}
但我不明白这会导致输入法不显示。
下面的方法全部调用return true
不管我是否设置setTextIsSelectable
editText.isFocusable(); // true
editText.isFocusableInTouchMode(); // true
editText.isClickable(); // true
editText.isLongClickable(); // true
我问这个问题,一方面是因为我很好奇,另一方面是因为我需要在我的应用程序中禁用系统键盘。我想了解正在发生的事情,以便我可以确定它确实在按照我的想法行事。
更新
后续问答:
- How to enable keyboard on touch after disabling it with setTextIsSelectable
当您调用 TextView.setTextIsSelectable(true)
然后调用 select 文本作为 Editor.startSelectionActionModeInternal
的结果时键盘不显示,它检查 TextView.isTextSelectable
是否为 false
在显示文本 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);
}
}
我猜为什么要这样实现可能是因为框架团队决定他们希望文本 Editor
可以预测 select 不会因为键盘而在屏幕上跳来跳去移动它。这可能只是一个 UI 的决定。据 git 称,自 2012 年以来一直如此,但该提交并未具体提及有关该实施的任何内容。