Hide Soft Keyboard on Done Keypress in Android?

前端 未结 6 1444
盖世英雄少女心
盖世英雄少女心 2021-02-01 16:26

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

相关标签:
6条回答
  • 2021-02-01 16:49

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

    <EditText
        ...
        android:imeOptions="actionDone" />
    
    0 讨论(0)
  • 2021-02-01 16:49

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

    0 讨论(0)
  • 2021-02-01 16:50

    SoftKeyboard can be hide by following way

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

    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.
            }
    
    0 讨论(0)
  • 2021-02-01 17:03

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

     <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" />
    
    0 讨论(0)
  • 2021-02-01 17:04
    InputMethodManager inputManager = (InputMethodManager)
    context.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.toggleSoftInput(0, 0);
    

    with context being your activity.

    0 讨论(0)
  • 2021-02-01 17:07

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

    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.

    0 讨论(0)
提交回复
热议问题