Android 键盘弹出与隐藏

 //1.弹出软键盘
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

 //2.弹出软键盘
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);


//关闭(如果是在Dialog中弹出的,则一定要在Dialog关闭前关闭软键盘;如果是在Activity中弹出的,则一定要在finish之前关闭软键盘)

 //1.隐藏软键盘
                InputMethodManager inputMgr = (InputMethodManager) context
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMgr.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);

/**
     * 2.隐藏软键盘(有输入框)
     * @param context
     * @param mEditText
     */
    public static void hideSoftKeyboard(@NonNull Context context, 
                    @NonNull EditText mEditText)
    {
        InputMethodManager inputmanger = (InputMethodManager) context
                 .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputmanger.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
    }


/**
     * 3.隐藏软键盘(有输入框)
     * @param context
     * @param mEditText
     */
    public static void hideSoftKeyboard(@NonNull Context context, 
                    @NonNull EditText mEditText)
    {
        InputMethodManager inputmanger = (InputMethodManager) context
                 .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputmanger.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
    }


你可能感兴趣的:(android)