我有一个 EditText
,它可以输入字母和其他符号,但不能输入数字。此外,我在 setOnKeyListener
上设置了 EditText
以通过按 Enter 关闭虚拟键盘。观察并发现由于以下代码,它无法接受数字输入
设置虚拟键盘回车后隐藏的代码
durOnTreadmill.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(durOnTreadmill.getWindowToken(), 0);
}
return true;
}
});
我的 XML EditText
<EditText
android:id="@+id/durOnTreadmill"
android:layout_width="129dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="32dp"
android:inputType="text"
android:hint="hh:mm:ss" />
我哪里错了?
最佳答案
在这里复制我的评论:
You should only return true from onKey if you're handling the input. I.e., move the return true inside your if block and return false otherwise.
关于android - 由于 setOnKeyListener,Edittext 不接受数字输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15670156/