How to close Android Soft KeyBoard programmatically?

前端 未结 14 1314
别跟我提以往
别跟我提以往 2020-12-02 16:33

I am currently showing softkeyboard using the following code

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


        
14条回答
  •  温柔的废话
    2020-12-02 17:20

    If you want, you can use entire class and call KeyboardUtil.hideKeyBoard(context) method wherever:

    public class KeyboardUtil
    {
    public static void hideKeyboard(Activity activity)
        {
            try
            {
                InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
             }
            catch (Exception e)
            {
                // Ignore exceptions if any
                    Log.e("KeyBoardUtil", e.toString(), e);
            }
        }
    }
    

提交回复
热议问题