Android 输入法键盘管理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010872619/article/details/78054061
/**
 * @author : TJ
 * @date : 2017/9/18 15:00
 * @description :键盘管理
 */

public class KeyboardUtils {

    /**
     * 改变键盘输入法的状态,如果已经弹出就关闭,如果关闭了就强制弹出
     */
    public static void chageInputState(Context context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 强制关闭软键盘
     */
    public static void closeKeyboard(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive() && view != null) {
            if (view.getWindowToken() != null) {
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }
    }

    /**
     * 强制弹出软键盘
     */
    public static void showKeyboard(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
    }
}

KeyboardUtils.closeKeyboard(getActivity(), getActivity().getWindow().getCurrentFocus());//强制关闭软件盘

猜你喜欢

转载自blog.csdn.net/u010872619/article/details/78054061