Dismiss keyboard when click outside of EditText in android

后端 未结 9 1107
旧时难觅i
旧时难觅i 2021-02-01 05:31

I have an EditText called myTextview. I want the soft keyboard to show when I click on the EditText but then dismiss if I click outside of

9条回答
  •  我在风中等你
    2021-02-01 06:04

    Maybe a little bit easier:

    Set a focusChangedListener on your edit text and then just hide the keyboard if you don't have focus.

    yourEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
    
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus){
                hideKeyboard();
            }               
        }
    });
    
    private void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);
    }
    

提交回复
热议问题