键盘相关→KeyboardUtils

 
   
  import android.app.Activity;
  import android.content.Context;
  import android.util.Log;
  import android.view.View;
  import android.view.inputmethod.InputMethodManager;
  import android.widget.EditText;
   
  /**
  * <pre>
  * author: Blankj
  * blog : http://blankj.com
  * time : 2016/8/2
  * desc : 键盘相关工具类
  * </pre>
  */
  public final class KeyboardUtils {
   
  private KeyboardUtils() {
  throw new UnsupportedOperationException("u can't instantiate me...");
  }
   
  /**
  * 避免输入法面板遮挡
  * <p>在manifest.xml中activity中设置</p>
  * <p>android:windowSoftInputMode="adjustPan"</p>
  */
   
  /**
  * 动态隐藏软键盘
  *
  * @param activity activity
  */
  public static void hideSoftInput(Activity activity) {
  View view = activity.getCurrentFocus();
  if (view == null) view = new View(activity);
  InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
  if (imm == null) return;
  imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
  }
   
  /**
  * 动态隐藏软键盘
  *
  * @param context 上下文
  * @param view 视图
  */
  public static void hideSoftInput(Context context, View view) {
  InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
  if (imm == null) return;
  imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
  }
   
  /**
  * 点击屏幕空白区域隐藏软键盘
  * <p>根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘</p>
  * <p>需重写dispatchTouchEvent</p>
  * <p>参照以下注释代码</p>
  */
  public static void clickBlankArea2HideSoftInput() {
  Log.d("tips", "U should copy the following code.");
  /*
  @Override
  public boolean dispatchTouchEvent(MotionEvent ev) {
  if (ev.getAction() == MotionEvent.ACTION_DOWN) {
  View v = getCurrentFocus();
  if (isShouldHideKeyboard(v, ev)) {
  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
  }
  }
  return super.dispatchTouchEvent(ev);
  }
 
  // 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘
  private boolean isShouldHideKeyboard(View v, MotionEvent event) {
  if (v != null && (v instanceof EditText)) {
  int[] l = {0, 0};
  v.getLocationInWindow(l);
  int left = l[0],
  top = l[1],
  bottom = top + v.getHeight(),
  right = left + v.getWidth();
  return !(event.getX() > left && event.getX() < right
  && event.getY() > top && event.getY() < bottom);
  }
  return false;
  }
  */
  }
   
  /**
  * 动态显示软键盘
  *
  * @param edit 输入框
  */
  public static void showSoftInput(EditText edit) {
  edit.setFocusable(true);
  edit.setFocusableInTouchMode(true);
  edit.requestFocus();
  InputMethodManager imm = (InputMethodManager) Utils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
  if (imm == null) return;
  imm.showSoftInput(edit, 0);
  }
   
  /**
  * 切换键盘显示与否状态
  */
  public static void toggleSoftInput() {
  InputMethodManager imm = (InputMethodManager) Utils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
  if (imm == null) return;
  imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
  }
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值