Android dismiss keyboard

后端 未结 9 504
面向向阳花
面向向阳花 2021-01-29 23:51

How do I dismiss the keyboard when a button is pressed?

9条回答
  •  醉酒成梦
    2021-01-30 00:27

    The first solution with InputMethodManager worked like a champ for me, the getWindow().setSoftInputMode method did not on android 4.0.3 HTC Amaze.

    @Ethan Allen, I did not need to make the edit text final. Maybe you are using an EditText inner class that you declared the containing method? You could make the EditText a class variable of the Activity. Or just declare a new EditText inside the inner class / method and use findViewById() again. Also, I didn't find that I needed to know which EditText in the form had focus. I could just pick one arbitrarily and use it. Like so:

        EditText myEditText= (EditText) findViewById(R.id.anyEditTextInForm);  
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
    

提交回复
热议问题