首页 > 其他 > 详细

键盘操作工具类KeyBoardUtils

时间:2019-10-31 17:24:56      阅读:143      评论:0      收藏:0      [点我收藏+]

键盘操作工具类KeyBoardUtils


import android.annotation.SuppressLint;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class KeyboardUtils {
    public KeyboardUtils() {
    }

    public static void showKeyboard(Context context) {
        ((InputMethodManager)((InputMethodManager)context.getSystemService("input_method"))).toggleSoftInput(0, 1);
    }

    public static void showKeyboard(View view, Context context) {
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        InputMethodManager imm = (InputMethodManager)context.getSystemService("input_method");
        imm.showSoftInput(view, 1);
    }

    public static void toggleKeyboard(Context context) {
        ((InputMethodManager)((InputMethodManager)context.getSystemService("input_method"))).toggleSoftInput(1, 0);
    }

    public static void hideKeyboard(Context ctx, View view) {
        if (ctx != null && view != null) {
            view.clearFocus();
            InputMethodManager imm = (InputMethodManager)ctx.getSystemService("input_method");
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    public static boolean isOpenInput(Context ctx, EditText etx) {
        InputMethodManager imm = (InputMethodManager)ctx.getSystemService("input_method");
        return imm.isActive(etx);
    }

    public static void registerActionHideInputListener(final Context context, final View view) {
        view.setOnTouchListener(new OnTouchListener() {
            @SuppressLint({"ClickableViewAccessibility"})
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == 0) {
                    KeyboardUtils.hideKeyboard(context, view);
                }

                return false;
            }
        });
    }
}

键盘操作工具类KeyBoardUtils

原文:https://www.cnblogs.com/Ricardoldc/p/11772136.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!