在Android中完成按键时隐藏软键盘? [英] Hide Soft Keyboard on Done Keypress in Android?
问题描述
我正在努力使用软键盘上的完成按钮.我无法按下软键盘完成"键来隐藏键盘.通过另一个按钮,它与
I'm struggling with the done button on the soft keyboard. I can't get the soft keyboard Done key press to hide the keyboard. From another button, it works perfectly with
imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
但是onKeyListener不能按我想要的方式运行.当我按下editText时,软键盘出现,并且其内容已从字符中清除.
but the onKeyListener does not function the way I want. When I hit the editText, the soft keyboard shows up and its content is cleared from characters.
感谢您的收听!
main.xml:
<EditText
android:id="@+id/answer"
android:layout_gravity="center_horizontal" android:textSize="36px"
android:inputType="phone"
android:minWidth="60dp" android:maxWidth="60dp"
/>
Java文件:
private EditText editText;
//...
editText = (EditText)findViewById(R.id.answer);
editText.setOnClickListener(onKeyboard);
editText.setOnKeyListener(onSoftKeyboardDonePress);
//...
// method not working:
private View.OnKeyListener onSoftKeyboardDonePress=new View.OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
{
// code to hide the soft keyboard
imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
}
return false;
}
};
private View.OnClickListener onKeyboard=new View.OnClickListener()
{
public void onClick(View v)
{
editText.setText("");
}
};
使用按钮的工作方法(在同一java文件中):
The working method using a button (in the same java file):
private View.OnClickListener onDone=new View.OnClickListener()
{
public void onClick(View v)
{
//....
// code to hide the soft keyboard
imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
}
};
编辑:当我按"9"键时,键盘隐藏了.真奇怪.
When I press key no "9" the keyboard hides. That's odd.
推荐答案
使用android:imeOptions ="actionDone",如下所示:
Use android:imeOptions="actionDone", like that:
<EditText
...
android:imeOptions="actionDone" />
这篇关于在Android中完成按键时隐藏软键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!