如何阻止 Android 软键盘出现在我的整个应用程序中
我正在硬件设备上开发一个应用程序,该设备具有内置硬件键盘,该键盘不会滑出,因此始终可见(如黑莓)。因此,我从不希望为我的整个应用程序显示软键盘。我知道另一个SO问题给出了以下内容代码行:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
但我不知道在哪里放置此代码以将软键盘隐藏在它可能出现在我的活动中的所有位置。我尝试将代码添加到 Activity.onUserInteraction
但无济于事。键盘似乎在 onUserInteraction
执行之后出现。
我还尝试将以下内容添加到我的
中:
<activity
android:windowSoftInputMode="stateAlwaysHidden"
>
软键盘仍然出现。
I'm developing an application on a hardware device that has a built-in hardware keyboard that does not slide out so is always visible (like a blackberry). Therefore, I NEVER want the soft keyboard to display for my entire application. I'm aware of another SO question that gives the following lines of code:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
But I don't know where to put this code to hide the soft keyboard in all places where it might possibly appear in my Activity. I have tried adding the code to Activity.onUserInteraction
to no avail. It seems the keyboard appears after the onUserInteraction
executes.
I also tried adding the following to my <Activity>
:
<activity
android:windowSoftInputMode="stateAlwaysHidden"
>
Soft keyboard still appears.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的应用程序不应该执行任何操作。设备的固件应包含一个基于硬件键盘可见而禁止软键盘的配置,就像所有其他具有硬件键盘的 Android 设备一样。如果这种情况没有发生,请与硬件制造商联系,看看他们是否计划解决这个问题。
Your application should not do anything. The device's firmware should contain a configuration that inhibits the soft keyboard based on the hardware keyboard being visible, just like every other Android device that has a hardware keyboard. If that is not happening, talk to the hardware maker and see if they are planning on addressing this.
明天演示的一个简单解决方法:
我将创建一个带有空视图的新 IME。这里有两个开源项目供您查看一些代码。
如果您想了解有关输入法的更多信息,请访问 创建输入法。
An easy workaround for tomorrow presentation:
I would create a new IME with an empty view. Here are two openSource projects for you to look at some code.
If you want to know more about input methods, go to Creating an input method.
如果 EditText 的 inputType 为 0,则选择该 EditText 时软键盘将永远不会弹出。
当然,您需要对应用程序中的所有 EditText 执行此操作,或者您始终可以子类化 EditText 并在构造函数中将输入类型设置为 0。
设置 xml inputType 参数不起作用,因为这对应于对 setRawInputType 方法的调用,该方法不会删除 KeyListener。
If an EditText has an inputType of 0, the soft keyboard will never pop up when that EditText is selected.
This will of course need to be done for all the EditTexts in your application, or you could always subclass EditText and set the input type to 0 in your constructor.
Setting the xml inputType parameter will not do, since that corresponds to a call to the setRawInputType method, which does not remove the KeyListener.
我通过在有点自定义的 EditText 中重写 onCheckIsTextEditor 方法解决了这个问题。
I solved it by overriding onCheckIsTextEditor method in my a-bit-custom EditText.
无论您在哪里编辑文本,请将此代码放入..
Where ever you have Edit text, put this code..