android 键盘工具类,键盘操作工具类KeyBoardUtils

键盘操作工具类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;

}

});

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值