android 在按下回车键时设置隐藏键盘(在 EditText 中)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2434532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:50:47  来源:igfitidea点击:

android set hidden the keyboard on press enter (in a EditText)

androidandroid-softkeyboard

提问by Martin Magakian

When my user press Enteron the virtual android "user validate entry!" keyboard my keyboard stay visible! (Why?)

当我的用户按下Enter虚拟 android 时“用户验证输入!” 键盘我的键盘保持可见!(为什么?)

Here my Java code...

这是我的 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!");
}

... here my View

...这里我的观点

 <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>

Maybe something wrong on my virtual device?

也许我的虚拟设备有问题?

回答by jqpubliq

This should do it:

这应该这样做:

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;
        }
    });

回答by Micha Okkerman

Keep the singleLine="true" and add imeOptions="actionDone" to the EditText. Then in the OnEditorActionListener check if actionId == EditorInfo.IME_ACTION_DONE, like so (but change it to your implementation):

保持 singleLine="true" 并将 imeOptions="actionDone" 添加到 EditText。然后在 OnEditorActionListener 中检查 actionId == EditorInfo.IME_ACTION_DONE 是否如此(但将其更改为您的实现):

if (actionId == EditorInfo.IME_ACTION_DONE) {

                if ((username.getText().toString().length() > 0)
                        && (password.getText().toString().length() > 0)) {
                    // Perform action on key press
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(username.getWindowToken(),
                            0);
                    doLogin();
                }
            }

回答by Hans

If you make the text box a single line (I believe the propery is called SingleLine in the layout xml files) it will exit out of the keyboard on enter.

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

Here you go: http://developer.android.com/reference/android/R.styleable.html#TextView_singleLine

给你:http: //developer.android.com/reference/android/R.styleable.html#TextView_singleLine

回答by Ricardo Ruas

I am create a custom component who extends AutoCompleteTextView, like in the example below:

我正在创建一个扩展 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);
}

I am using this code in the AlertDialog.Builder, but is possible to be using to Activity.

我在 AlertDialog.Builder 中使用此代码,但可以用于 Activity。

回答by Satyajit Das

just add this line in your edit text.

只需在您的编辑文本中添加这一行。

android:imeOptions="actionDone"'

you can specify the next edit text id to move to that edit text on click of the keyboard done button.

您可以指定下一个编辑文本 id 以通过单击键盘完成按钮移动到该编辑文本。