Android:禁用所有 EditText 的软键盘
我正在 Android 上使用一些 EditText
开发一个对话框。 我已将此行放在 onCreate()
中,以便禁用软键盘:
Keypad.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
问题是它仅在对话框出现且不执行任何操作时才起作用。 当我移动到下一个 EditText
时,键盘会出现并且不会向下移动。
有人知道如何解决这个问题吗?
I am working on a dialog at Android with a few EditText
s.
I've put this line at the onCreate()
in order to disable the soft keyboard:
Keypad.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
The problem is that it works only when the dialog appear and doing nothing.
When I move to the next EditText
, the keyboard appears and not going down.
Does anybody have an idea how to solve this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
创建您自己的类来扩展
EditText
并覆盖onCheckIsTextEditor()
:create your own class that extends
EditText
and override theonCheckIsTextEditor()
:试试这个..
Try this out..
我一整天都在寻找解决方案,并且遇到了这种方法。我把它放在这里是因为它似乎完美地回答了这个问题。
不需要子类化。这与使 EditText 不可聚焦之间的主要区别在于,EditText 仍然有自己的光标 - 您可以选择文本等。它所做的只是抑制 IME 弹出自己的软键盘。
I have been looking for solutions to this all day, and I came across this approach. I'm putting it here because it seems to answer this question perfectly.
No need to subclass. The main difference between this and making your EditText non-focusable, is that the EditText still has its own cursor - you can select text, etc. All it does is suppress the IME from popping up its own soft keyboard.
调用
TextView.setShowSoftInputOnFocus(false)
。此方法自 API 级别 21 起就有记录,但自 API 级别 16 起就已存在(只是对 JavaDoc 隐藏了)。我在 AlertDialog 中将其与 API 级别 16 结合使用dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
。在API级别14中,有一个隐藏方法
setSoftInputShownOnFocus()
,它似乎具有相同的目的,但我没有测试过。相对于
InputType.TYPE_NULL
的优点是,可以使用所有正常输入类型(例如密码输入),并且触摸事件将光标定位在文本内的正确位置。Call
TextView.setShowSoftInputOnFocus(false)
. This method is documented since API level 21, but it's already there since API level 16 (it's just hidden from JavaDoc). I use it with API level 16 in an AlertDialog in combination withdialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
.In API level 14, there is a hidden method
setSoftInputShownOnFocus()
which seems to have the same purpose, but I have not tested that.The advantage over
InputType.TYPE_NULL
is, that all the normal input types can be used (e.g. for password input) and the touch event positions the cursor at the correct spot within the text.这篇文章发布已经有一段时间了,但这里有一个对我有用的简单方法:在 xml 的
EditText
属性中,执行:android:focusable="false"
。现在即使用户点击键盘也不会弹出。如果您提供自己的键盘,这非常有用。Its been some time since this post, but here is a simple method which worked for me: in the xml, in the
EditText
properties, do:android:focusable="false"
. Now the keyboard will not popup even if the user clicks on it. This is useful if you are providing your own keypad.将其放入 Oncreate 方法中
Put this in Oncreate Method
知道为时已晚,但是您是否在布局中尝试过对 EditText 进行以下设置?
更新
使用,
Know its too late, but have you tried the following setting to EditText in your layout ?
UPDATE
Use,
为了禁用 ANDROID SOFT INPUT KEYBOARD xml 文件对我来说没有帮助
在 java 文件中的 EditText 对象上调用 setInputType 方法效果很好。
这是代码。
in order to disable ANDROID SOFT INPUT KEYBOARD xml file doesn't help in my case
calling the setInputType method on EditText object in java file works great.
here is the code.
如果将 textView 放入视图组中,则可以使用以下命令使视图在其任何后代之前获得焦点:
If you put the textViews in the view group you can make make the view get the focus before any of its descendants by using this:
通过设置EditText focusable->false,点击时键盘不会打开
by set EditText focusable->false, keyboard will not opened when clicked
您可以这样做:
它将禁用 textView 的键盘,并且单击将不再起作用。
You can do that:
It will disable the keyboard to the textView and the click will not work anymore.
如果您查看 onCheckIsTextEditor() 方法实现(在 TextView 中),它看起来像这样:
这意味着您不必子类化,您可以:
我尝试设置 android:inputType="none" 在布局 xml 中,但它对我不起作用,所以我以编程方式完成了它。
If you take look on onCheckIsTextEditor() method implementation (in TextView), it looks like this:
This means you don't have to subclass, you can just:
I tried setting android:inputType="none" in layout xml but it didn't work for me, so I did it programmatically.