我正在使用 custom in-app keyboard ,所以我需要禁用系统键盘。我可以用
editText.setShowSoftInputOnFocus(false);
适用于 Android API 21+。但是要对 API 11 做同样的事情,我正在做
editText.setTextIsSelectable(true);
有时我想在使用 setTextIsSelectable
禁用系统键盘后再次显示它。但我无法弄清楚如何。执行以下操作确实会显示系统键盘,但是如果用户隐藏键盘然后再次单击 EditText,键盘仍然不会显示。
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, 0);
我想我可以执行 editText.setOnFocusChangeListener
然后手动显示或隐藏系统键盘,但我更愿意撤消 setTextIsSelectable
所做的任何操作。以下也不起作用:
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.setClickable(true);
editText.setLongClickable(true);
我该怎么做?
最佳答案
简答
执行以下操作将反转 setTextIsSelectable(true)
的效果,并允许键盘在 EditText
获得焦点时再次显示。
editText.setTextIsSelectable(false);
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.setClickable(true);
editText.setLongClickable(true);
editText.setMovementMethod(ArrowKeyMovementMethod.getInstance());
editText.setText(editText.getText(), TextView.BufferType.SPANNABLE);
解释
阻止键盘显示的是 isTextSelectable()
为 true
。你可以看到 here (感谢 @adneal)。
setTextIsSelectable
的源码是
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();
}
因此,上面简短回答部分的代码首先使用setTextIsSelectable(false)
将mTextIsSelectable
设置为false
,然后撤消所有其他副作用一一说明。
关于android - 如何在使用 setTextIsSelectable 禁用键盘后启用触摸键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45228531/