我有一个 EditText,它不允许用户通过键盘(软键盘或硬键盘)输入任何内容。此 EditText 应仅允许用户通过应用程序在屏幕上显示的键(按钮)输入内容。 我已经禁用了软键盘,但我找不到禁用硬件键盘输入的方法。这种通过硬件键盘的输入可以使用配置为允许通过硬件键盘输入的模拟器来完成。 所以,我的问题是,如何在 EditText 中阻止通过物理键盘输入? 谢谢!
最佳答案
终于明白了!
代码:
editText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
return true;
}
});
解释:
返回 true 将告诉听众我已经处理了硬件键盘输入。基于 Android 文档 ( http://developer.android.com/reference/android/view/View.OnKeyListener.html )
View.OnKeyListener: Interface definition for a callback to be invoked when a hardware key event is dispatched to this view. The callback will be invoked before the key event is given to the view. This is only useful for hardware keyboards; a software input method has no obligation to trigger this listener.
onKey(View v, int keyCode, KeyEvent event) Called when a hardware key is dispatched to a view.
关于android - 在 EditText 中阻止物理键盘输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34185722/