how to hide keyboard after typing in EditText in a

2019-01-13 00:34发布

I have a EditText and button aligned to parent's bottom.

When I enter text in it and press the button to save data, the virtual keyboard does not disappear.

Can any one guide me how to hide the keyboard?

14条回答
虎瘦雄心在
2楼-- · 2019-01-13 01:06
editText.setInputType(InputType.TYPE_NULL);
查看更多
老娘就宠你
3楼-- · 2019-01-13 01:07

This should work.

InputMethodManager inputManager = 
        (InputMethodManager) context.
            getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(
        this.getCurrentFocus().getWindowToken(),
        InputMethodManager.HIDE_NOT_ALWAYS); 

Just make sure that this.getCurrentFocus() does not return null, which it would if nothing has focus.

查看更多
闹够了就滚
4楼-- · 2019-01-13 01:08

I did not see anyone using this method:

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean focused) {
        InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (focused)
            keyboard.showSoftInput(editText, 0);
        else
            keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }
});

And then just request focus to the editText:

editText.requestFocus();
查看更多
在下西门庆
5楼-- · 2019-01-13 01:10

Been struggling with this for the past days and found a solution that works really well. The soft keyboard is hidden when a touch is done anywhere outside the EditText.

Code posted here: hide default keyboard on click in android

查看更多
你好瞎i
6楼-- · 2019-01-13 01:12
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
查看更多
走好不送
7楼-- · 2019-01-13 01:13
int klavStat = 1; // for keyboard soft/hide button
int inType;  // to remeber your default keybort Type

editor - is EditText field

/// metod for onclick button ///
 public void keyboard(View view) {
        if (klavStat == 1) {
            klavStat = 0;

            inType = editor.getInputType();

            InputMethodManager imm = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);


            editor.setInputType(InputType.TYPE_NULL);

            editor.setTextIsSelectable(true);



        } else {
            klavStat = 1;


            InputMethodManager imm = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);

                imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);

            editor.setInputType(inType);

        }
    }

If u have anather EditText Field, u need to watch for focus change

查看更多
登录 后发表回答