Android 软键盘的显示和隐藏

Android 软键盘的显示和隐藏

核心方法

显示软键盘:

InputMethodManager#showSoftInput(View view, int flags)

隐藏软键盘:

InputMethodManager#hideSoftInputFromWindow(IBinder windowToken, int flags)

flags操作标志:

flags 0 SHOW_IMPLICIT SHOW_FORCED
0 Y Y Y
HIDE_IMPLICIT_ONLY N Y N
HIDE_NOT_ALWAYS Y Y N

先弹出软键盘,再隐藏软键盘,Y 表示软键盘隐藏,N 表示软件不能隐藏。

SHOW_IMPLICIT:隐式弹窗键盘,表示弹出键盘不是由用户直接发起的,键盘可能不会弹出。

SHOW_FORCED:强制弹出键盘,表示弹出键盘是用户直接发起的,在用户请求收起键盘前,软键盘会一直显示。

HIDE_IMPLICIT_ONLY:只有通过 SHOW_IMPLICIT 弹出的键盘才能被隐藏。

HIDE_NOT_ALWAYS:表示只要不是通过 SHOW_FORCED 弹出的键盘都会被隐藏。

调用 SHOW_FORCED 可以保证软键盘一定会弹出,调用 0 可以保证软键盘一定会隐藏。

也可以简单粗暴的将两个方法中的 flag 都设为 0。

软键盘工具类

/**
 * 软键盘工具类
 */
public class KeyboardUtils {
   

    /**
     * 显示软键盘
     *
     * @param editText
     */
    public static void showSoftInput(EditText editText) {
   
        if (editText == null)
            return;
        InputMethodManager imm = (InputMethodManager) BaseApplication.getInstance().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null)
            return;
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        imm.showSoftInput(editText, 0);
    }

    /**
     * 隐藏软键盘
     *
     * @param view
     */
    public static void hideSoftInput(View view) {
   
        if (view == null)
            return;
        InputMethodManager inputMethodManager = (InputMethodManager) BaseApplication.getInstance().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager == null)
            return;
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * 隐藏软键盘
     *
     * @param editText
     */
    public static void hideSoftInput(EditText editText) {
   
        if (editText == null)
            return;
        InputMethodManager inputMethodManager = (InputMethodManager) BaseApplication.getInstance().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager == null)
            return;
        editText.clearFocus();
        inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }

    /**
     * 隐藏软键盘
     *
     * @param activity
     */
    public static void hideSoftInput(Activity activity) {
   
        Window window = activity.getWindow();
        View view = window.getCurrentFocus();
        if (view == null) {
   
            view = window.getDecorView();
        }
        hideSoftInput(view);
    }

    /**
     * 软键盘切换
     */
    public static void toggleSoftInput() {
   
        InputMethodManager imm = (InputMethodManager) BaseApplication.getInstance().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null)
            return;
        imm.toggleSoftInput(0, 0);
    }

}

点击空白区域隐藏软键盘

方式一

public class BaseActivity extends AppCompatActivity {
   

    @Override
    public boolean onTouchEvent(MotionEvent event) {
   
        if (getCurrentFocus() != null) {
   
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            if (mInputMethodManager != null) {
   
                return mInputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            }
        }
        return super.onTouchEvent(event);
    }
}

方式二

public class BaseActivity extends AppCompatActivity {
   

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
   
//        if (getCurrentFocus() != null) {
   
//            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
//            if (mInputMethodManager != null) {
   
//                return mInputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
//            }
//        }
//        return super.onTouchEvent(event);
//    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
   
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
   
            View view = getCurrentFocus();
            if (isShouldHideKeyboard(view, ev)) {
   
                KeyboardUtils.hideSoftInput(view);
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 是否隐藏软键盘
     */
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
   
        if (v != null && v instanceof EditText) {
   
            int[] location = {
   0, 0};
            v.getLocationInWindow(location);
            int left = location[0];
            int top = location[1];
            int right = left + v.getWidth();
            int bottom = top + v.getHeight();
            return !(event.getX() > left && event.getY() < right && event.getY() > top && event.getY() < bottom);
        }
        return false;
    }
}

相关推荐

  1. Android 键盘显示隐藏

    2023-12-29 00:30:05       37 阅读
  2. android 自定义键盘显示隐藏

    2023-12-29 00:30:05       39 阅读
  3. css元素隐藏显示

    2023-12-29 00:30:05       26 阅读
  4. Android 8.1 实体键盘输入时收起键盘

    2023-12-29 00:30:05       19 阅读
  5. React 抽屉显示隐藏

    2023-12-29 00:30:05       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-29 00:30:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-29 00:30:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-29 00:30:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-29 00:30:05       20 阅读

热门阅读

  1. 如何获取2024年交易日历?

    2023-12-29 00:30:05       42 阅读
  2. Hive和Spark生产集群搭建(spark on doris)

    2023-12-29 00:30:05       25 阅读
  3. 八股文打卡day13——计算机网络(13)

    2023-12-29 00:30:05       28 阅读