如果我使用包含单个 EditText 的单个 Activity 创建一个简单的应用程序,并且我这样做
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);
那么这会阻止键盘出现(在我对 Android 5.0 和 7.1 的测试中)。这就是我想要的,正如这些问题所要求的:
- 从 EditText 禁用软键盘但仍允许复制/粘贴? https://stackoverflow.com/questions/15174606/disable-soft-keyboard-from-edittext-but-still-allow-copy-paste
- 如何针对特定活动禁用 Android 软键盘? https://stackoverflow.com/questions/5851339/how-to-disable-android-soft-keyboard-for-a-particular-activity
- Android:禁用所有 EditText 的软键盘 https://stackoverflow.com/questions/5803193/android-disable-soft-keyboard-at-all-edittexts
- 如何在编辑文本时禁用键盘弹出窗口? https://stackoverflow.com/questions/10611833/how-to-disable-keypad-popup-when-on-edittext
- 在 EditText 上禁用键盘 https://stackoverflow.com/questions/10636635/disable-keyboard-on-edittext
The 源代码 https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/TextView.java#L5607 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();
}
但我不明白这会禁用输入法的出现。
调用以下方法均返回true
我是否设置setTextIsSelectable
or not.
editText.isFocusable(); // true
editText.isFocusableInTouchMode(); // true
editText.isClickable(); // true
editText.isLongClickable(); // true
我问这个问题的部分原因是我很好奇,但也因为我需要在我的应用程序中禁用系统键盘。我想了解正在发生的事情,以便我可以确定它确实在做我认为它正在做的事情。
Update
后续问题及解答:
- 如何在使用 setTextIsSelectable 禁用触摸键盘后启用键盘 https://stackoverflow.com/questions/45228531/how-to-enable-keyboard-on-touch-after-disabling-it-with-settextisselectable
拨打电话时不显示键盘TextView.setTextIsSelectable(true)
然后选择文本作为结果Editor.startSelectionActionModeInternal https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/Editor.java#L2009,它检查是否TextView.isTextSelectable
is false
在显示文本选择之前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
可以预测可以选择不因键盘移动屏幕而在屏幕上跳跃。这可能只是 UI 的决定。根据 git 的说法,自 2012 年以来一直如此,但提交没有提及任何具体与该实现相关的内容。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)