在 Android 中的 Done Keypress 上隐藏软键盘?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3413157/
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 10:05:01  来源:igfitidea点击:

Hide Soft Keyboard on Done Keypress in Android?

androidandroid-softkeyboardsoft-keyboard

提问by Benny Skogberg

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

我正在努力使用软键盘上的完成按钮。我无法使用软键盘 Done key press 来隐藏键盘。从另一个按钮,它与

imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);

imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);

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.

但是 onKeyListener 没有按照我想要的方式运行。当我点击 editText 时,软键盘出现并且其内容从字符中清除。

Thanks for listening!

谢谢收听!

The main.xml:

main.xml:

<EditText 
    android:id="@+id/answer" 
    android:layout_gravity="center_horizontal" android:textSize="36px"
    android:inputType="phone"
    android:minWidth="60dp" android:maxWidth="60dp"
/>

The Java file:

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

The working method using a button (in the same java file):

使用按钮的工作方法(在同一个java文件中):

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

Edit:When I press key no "9" the keyboard hides. That's odd.

编辑:当我按下“9”键时,键盘会隐藏。这很奇怪。

回答by alcsan

Use android:imeOptions="actionDone", like that:

使用 android:imeOptions="actionDone",就像这样:

<EditText
    ...
    android:imeOptions="actionDone" />

回答by Doge

InputMethodManager inputManager = (InputMethodManager)
context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0, 0);

with context being your activity.

上下文是您的活动。

回答by Benny Skogberg

Changed the if-statement to if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)made it working with the xml-attribute android:inputType="phone".

更改 if 语句以if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)使其与 xml-attribute 一起使用android:inputType="phone"

回答by Stan

You should have a look at setOnEditorActionListener() for the EditText:

您应该查看 EditText 的 setOnEditorActionListener() :

Set a special listener to be called when an action is performed on the text view. This will be called when the enter key is pressed, or when an action supplied to the IME is selected by the user.

设置在文本视图上执行操作时要调用的特殊侦听器。这将在按下 Enter 键时调用,或者当用户选择提供给 IME 的操作时调用。

回答by Sagar Jethva

Use below code with android:imeOptions="actionDone"its work for me.

使用下面的代码android:imeOptions="actionDone"为我工作。

 <EditText
    android:id="@+id/et_switch_name"       
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="Name"
    android:imeOptions="actionDone"       
    android:inputType="textPersonName" />

回答by Vishv Shroff

SoftKeyboard can be hide by following way

软键盘可以通过以下方式隐藏

In Java class we can write following code to hide keyboard when user press done or enter

在 Java 类中,我们可以编写以下代码来在用户按下完成或输入时隐藏键盘

etBid.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                    actionId == EditorInfo.IME_ACTION_DONE ||
                    event != null &&
                            event.getAction() == KeyEvent.ACTION_DOWN &&
                            event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
            {
                if (event == null || !event.isShiftPressed())
                {
                    // the user is done typing.
                    InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    return true; // consume.
                }
            }
            return false; // pass on to other listeners.
        }