Android:用于不接收按键的 EditText 的 KeyListener
我有一个要监视 KeyEvents 的 EditText,并且设置了一个侦听器,如下所示:
mText = (EditText) this.findViewById(R.id.title);
mText.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
final int view = v.getId();
switch (view) {
case R.id.title:
Log.d(LOG_TAG, "key handled");
break;
}
return false;
}
});
我的问题是,当使用虚拟键盘输入 EditText 时,触发日志记录的唯一按键是退格键。我已经验证所有其他按键甚至都不会触发 onKey()
。我确信这很简单,但在 SO 上没有找到任何似乎可以解决这个问题的东西。
谢谢,
保罗
I have an EditText that I want to monitor KeyEvents for, and I have a listener set up as follows:
mText = (EditText) this.findViewById(R.id.title);
mText.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
final int view = v.getId();
switch (view) {
case R.id.title:
Log.d(LOG_TAG, "key handled");
break;
}
return false;
}
});
My problem is that when the EditText is being typed into using the virtual keyboard, the only key press that triggers the logging is the backspace key. I've verified that all other keypresses aren't even triggering onKey()
. I'm sure this is something simple, but didn't find anything on SO that seemed to deal with this.
Thanks,
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
addTextChangedListener(TextWatcher watcher)
定义 在这里使用它您可以处理物理键盘和软键盘。我希望它有帮助
Try using
addTextChangedListener(TextWatcher watcher)
defined here with it you can handle the physical and the soft keyboard.I hope it helps
来自 Android 参考:
http://developer.android.com/reference /android/view/View.OnKeyListener.html
View.OnKeyListener
类概述
当硬件按键事件分派到此视图时要调用的回调的接口定义。回调将在按键事件传递给视图之前调用。这仅对硬件键盘有用; 软件输入法没有义务触发此侦听器。
似乎 OnKeyListener 专门设计为仅对硬件按键做出反应!
From the Android reference at:
http://developer.android.com/reference/android/view/View.OnKeyListener.html
View.OnKeyListener
Class Overview
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.
It seems OnKeyListener is designed expressly to react to HARDWARE keys only!
onKeyListener
仅适用于:在
onCreate
方法中设置它。onKeyListener
only works with:Set it in the
onCreate
method.