android设置在按enter键时隐藏键盘(在编辑文本中)

android设置在按enter键时隐藏键盘(在编辑文本中),android,android-softkeyboard,Android,Android Softkeyboard,当我的用户在虚拟android“user validate entry!”键盘上按Enter键时,我的键盘保持可见!(为什么?) 这里是我的Java代码 private void initTextField() { entryUser = (EditText) findViewById(R.id.studentEntrySalary); entryUser.setOnKeyListener(new OnKeyListener() { public boolean

当我的用户在虚拟android“user validate entry!”键盘上按Enter键时,我的键盘保持可见!(为什么?)

这里是我的Java代码

private void initTextField() {
    entryUser = (EditText) findViewById(R.id.studentEntrySalary);
    entryUser.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_ENTER:
                        userValidateEntry();
                        return true;
                }
            }

          return true;
        }
    });
}

private void userValidateEntry() {
    System.out.println("user validate entry!");
}
。。。这是我的看法

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content">
            <EditText android:id="@+id/studentEntrySalary" android:text="Foo" android:layout_width="wrap_content" android:layout_height="wrap_content" />
 </LinearLayout>


我的虚拟设备可能出了问题?

如果将文本框设为单行(我相信该属性在布局xml文件中称为单行),它将在输入时退出键盘

给你:

这应该可以:

yourEditTextHere.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

                // NOTE: In the author's example, he uses an identifier
                // called searchBar. If setting this code on your EditText
                // then use v.getWindowToken() as a reference to your 
                // EditText is passed into this callback as a TextView

                in.hideSoftInputFromWindow(searchBar
                        .getApplicationWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
               userValidateEntry();
               // Must return true here to consume event
               return true;

            }
            return false;
        }
    });
保留singleLine=“true”并将imeOptions=“actionDone”添加到编辑文本中。 然后在OnEditorActionListener中检查actionId==EditorInfo.IME\u ACTION\u是否完成,如是(但将其更改为您的实现):


我正在创建一个扩展AutoCompleteTextView的自定义组件,如下例所示:

public class PortugueseCompleteTextView extends AutoCompleteTextView {
...
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (event != null &&  (event.getKeyCode() == KeyEvent.KEYCODE_BACK)) {
        InputMethodManager inputManager =
                (InputMethodManager) getContext().
                        getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(
                this.getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
    }
    return super.onKeyPreIme(keyCode, event);
}

我正在AlertDialog.Builder中使用此代码,但也可能用于活动。

只需在编辑文本中添加此行即可

android:imeOptions="actionDone"'

单击键盘上的“完成”按钮,可以指定下一个要移动到该编辑文本的编辑文本id。

否。android仍然存在同样的问题:singleLine=“true”这适用于我的android 4(Tablet 4.0.3,build target 4.2),以防它依赖于android版本(我没有在其他版本上尝试过)。你的a!谢谢(在这里更改搜索栏购买你的编辑文本)1。当我将代码更改为if(event!=null&&(event.getKeyCode()==KeyEvent.KEYCODE_D))时,代码似乎不起作用,因此在按下D之后,软输入表单不会隐藏。对于看到Samir注释的任何其他人,这是因为此代码设置了
OnEditorActionListener
,只有当按下类似Enter的键时才会调用它,而不是普通的字符键。如果我真的需要在“普通”字符按键上隐藏键盘?有什么想法吗?在我的例子中,
imeOptions=“actionDone”
就足够了,不需要代码。
android:imeOptions="actionDone"'