Clear edittext focus and hide keyboard when button is pressed

前端 未结 4 2158
伪装坚强ぢ
伪装坚强ぢ 2021-02-10 06:04

I\'m making an app with edittext and a button. When I enter something into edittext and then click a button, I want the keyboard and focus on edittext to disappear but I can\'t

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-10 06:38

    InputMethodManager inputManager = (InputMethodManager)
                                      getSystemService(Context.INPUT_METHOD_SERVICE); 
    
    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                         InputMethodManager.HIDE_NOT_ALWAYS);
    editText.setText("");
    

    Add the following on your button onclick event

    You need to import android.view.inputmethod.InputMethodManager;

    The keyboard hides and clears the text when you click the button.

提交回复
热议问题