View.OnKeyListener用于软键盘 [英] View.OnKeyListener for soft keyboard
问题描述
当某些特殊键被按下/触摸时,我正在使用View.OnKeyListener for EditText做某事.这是该代码的简化版本:
I am using the View.OnKeyListener for EditText to do something when some special keys are pressed/touched. Here is a shorthened version of the code:
public class MyKeyListener implements View.OnKeyListener{
// ...
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
// do some processing
return true;
} else if (keyCode == KeyEvent.KEYCODE_0) {
// do another processing
return true;
}
}
}
}
问题在于,根据文档,仅在使用硬键盘时才保证可以调用OnKeyListener.而且,的确,我在某些手机上测试了该功能,但这些功能不适用于软键盘.因此,我想知道是否有类似于该侦听器的东西可以用于软键盘.
The problem is that the OnKeyListener, according to the documentation, is only guaranteed to be called when hard keyboards are used. And, indeed, I tested it on some phones where it doesn't work for soft keyboards. So I was wondering if there is something similar to this listener that would work for soft keyboards.
推荐答案
不能保证,不能保证.
您可以使用TextWatcher
并将其附加到任何和所有EditTexts.
You can use a TextWatcher
and attach that to any and all EditTexts.
或者,您创建自己的KeyboardView
并强制用户使用该KeyboardView
,然后可以将期望的侦听器附加到该KeyboardView
. https://developer.android.com/guide/topics/text/creating-input-method.html?hl=ja#IMEUI
Or, you create your own KeyboardView
and force the user to use that one, then you can attach the expected listeners to it. https://developer.android.com/guide/topics/text/creating-input-method.html?hl=ja#IMEUI
这篇关于View.OnKeyListener用于软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!