我在一个 Activity 中有三个 EditText 框,其中两个正常输入法(硬键、默认软键盘)都可以。但是对于其中一个 EditText 框,我只想从自定义键盘 View 发送软输入。所以实际上我希望永远不会为此 EditText 显示默认的软键盘。我试过为 EditText 添加 onTouchListeners 和 onFocusChange 监听器,部分成功,如下所示:
public boolean onTouch(View v, MotionEvent event) {
v.requestFocus();
imm.toggleSoftInput(0, 0);
return true;
}
public void onFocusChange(View v, boolean hasFocus) {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive(v)) {
imm.toggleSoftInput(0,0);
}
}
但是我还没有得到一个确定的解决方案,因为
1)默认的软键盘总是在听众隐藏它之前短暂地闪烁可见
2)在某些情况下,例如使用硬键盘箭头键将焦点移动到 EditText 有时会设置默认软键盘可见
等等。
所以我很想找到一种简单的方法来告诉 Android 永远不要为这个特定的 EditText 显示默认的软键盘。我不想扩展 EditText 并开始覆盖一些东西,因为 EditText 功能对我来说是完美的——我只希望不显示默认的软键盘。
我已经花了好几天时间试图解决这个问题。通过谷歌找到的一些主题(包括此处的一些主题)对这个问题进行了半途而废的尝试,但到目前为止我还没有找到一个完整的功能解决方案。
编辑:
我真的开始生气了。我决定我可以尝试不使用 EditText,而是使用任何其他可以完成工作的 View 。事实证明,很难摆脱那个软键盘。当我使用硬键将焦点从 EditText 移动到 Button 时,它甚至会出现!为什么软键盘应该显示在每个恰好有焦点的怪胎 View 上?即使我明确说 inputType="none"?如何关闭 * 软键盘?下面是按钮的 xml - 让我们以此为例:
<Button
android:id="@+id/OkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="none"
android:paddingRight="5mm"
android:paddingLeft="5mm"
android:layout_below="@id/Volume"
android:layout_alignParentLeft="true"
android:text="OK"/>
编辑2:
我已经找到了一个似乎可行的解决方案。首先,我掌握了 InputMethodManager:
this.imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
我设置的 OnClickListener、OnTouchListener 和 OnFocusChange 监听器都在我希望 EditText 获得焦点并且我的自定义 KeyboardView 可见时调用以下方法,同时隐藏默认的软输入:
private boolean makeActive(View v) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
EditText e = (EditText) v;
int iType = e.getInputType();
e.setInputType(InputType.TYPE_NULL);
e.requestFocus();
showKb();
e.setInputType(iType);
return true;
}
最佳答案
简单
editText.setShowSoftInputOnFocus(false);
有些人建议以下内容可能适用于旧版本的 Android,但行为是意外的。
edittextPlay.setTextIsSelectable(true);
关于java - Android:如何永久且完全不显示 EditText 的默认软键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4880144/