其他分享
首页 > 其他分享> > Android 键盘输入管理

Android 键盘输入管理

作者:互联网

键盘输入管理

支持显示隐藏、自动切换、检查是否打开

InputMethod

import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

/**
 * 键盘输入法管理
 */
public class InputMethod {

    /**
     * 获取键盘管理器
     *
     * @param context 上下文
     * @return
     */
    public static InputMethodManager getInputMethodManager(Context context) {
        return (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    }

    /**
     * 键盘事发打开
     *
     * @param context 上下文
     * @return
     */
    public static boolean isActive(Context context) {
        return getInputMethodManager(context).isActive();
    }

    /**
     * 打卡软键盘
     *
     * @param context 上下文
     * @param v       控件
     */
    public static void show(Context context, View v) {
        InputMethodManager imm = getInputMethodManager(context);
        imm.showSoftInput(v, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    /**
     * 关闭软键盘
     *
     * @param context 上下文
     * @param v       控件
     */
    public static void hide(Context context, View v) {
        getInputMethodManager(context).hideSoftInputFromWindow(v.getWindowToken(), 0);
    }

    /**
     * 关闭显示输入法
     *
     * @param context 上下文
     */
    public static void toggle(Context context) {
        getInputMethodManager(context).toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

}

标签:Context,管理,键盘输入,param,InputMethodManager,context,Android,public,getInputMethodMa
来源: https://blog.csdn.net/u012127961/article/details/122492948