微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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);
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐