Android 虚拟键盘相关功能大全

移动开发跟虚拟键盘肯定要打交道,下来我们就说说常用的相关情况:

 

情况一、将键盘右下角的确认更改

android:imeOptions="actionDone" ,软键盘下方变成“完成”,
android:imeOptions="actionSend",软键盘下方变成“发送”
android:imeOptions="actionNext",软键盘下方变成“下一个”

//监听右下角按钮
mBinding.etSearch.setOnKeyListener((v, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
               
            return false;
        });

情况二、 当键盘弹起点击空白处关闭键盘

在需要使用的activity中使用,也可以在baseActivity中使用

     /**
     * 点击空白区域隐藏键盘.
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent me) {
        if (me.getAction() == MotionEvent.ACTION_DOWN) {  //把操作放在用户点击的时候
            View v = getCurrentFocus();
           if (isShouldHideKeyboard(v, me)) { //判断用户点击的是否是输入框以外的区域
                closeInputMethod(this);   //收起键盘
            }
        }
        return super.dispatchTouchEvent(me);
    }

    /**
     * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘,因为当用户点击EditText时则不能隐藏
     *
     * @param v
     * @param event
     * @return
     */
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {  //判断得到的焦点控件是否包含EditText
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],    //得到输入框在屏幕中上下左右的位置
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击位置如果是EditText的区域,忽略它,不收起键盘。
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略
        return false;
    }

     /**
     * 关闭键盘
     */
    public void closeInputMethod(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive() && activity.getCurrentFocus() != null) {
            if (activity.getCurrentFocus().getWindowToken() != null) {
                imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

情况三、监听键盘是否弹出

1、监听弹出隐藏以及获取键盘的高度

第一步:et.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
            //当键盘弹出隐藏的时候会 调用此方法。
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                //获取当前界面可视部分MainActivity.this.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                //获取屏幕的高度
                int screenHeight =  MainActivity.this.getWindow().getDecorView().getRootView().getHeight();
                //此处就是用来获取键盘的高度的, 在键盘没有弹出的时候 此高度为0 键盘弹出的时候为一个正数
                int heightDifference = screenHeight - r.bottom;
                Log.d("Keyboard Size", "Size: " + heightDifference);
//动态设置占位View的高
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) softInputSeat.getLayoutParams();
layoutParams.height = heightDifference;
softInputSeat.setLayoutParams(layoutParams);
            }
        });
第二步:AndroidManifest中配置:对应Activity配置:
android:windowSoftInputMode="stateAlwaysHidden"

2、监听弹出隐藏

    1、指定android:windowSoftInputMode="adjustResize|stateAlwaysHidden"这个的做法是为了让键盘弹出时改变布局。
    2、让Activity实现LayoutchangeListener,监听布局的改变,当布局发生的改变为屏幕的1/3
      @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > 0)) {//软键盘弹起
           LogUtil.e("//软键盘弹起 ");
       } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > 0)) {//软件盘关闭
            LogUtil.e("//软件盘关闭 ");
        }
    }

情况四、调起键盘和关闭键盘

1、隐藏键盘,两种方法任选其一

    /**
     * 关闭键盘
     */
    public void closeInputMethod(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive() && activity.getCurrentFocus() != null) {
            if (activity.getCurrentFocus().getWindowToken() != null) {
                imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }


    /**
     * 隐藏键盘
     */
    public void closeInputMethod(Activity activity, EditText v) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

2、显示键盘

注意:如果调用不起作用时===可以延迟调用,让view.requestFocus(),获取焦点的方法需要做线程之外调用否则会报错

     /**
     * 显示键盘
     */
    public void showSoftInputMethod(View view, Context mContext) {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            view.requestFocus();
            imm.showSoftInput(view, 0);
        }
    }
}

就先这些吧,后面遇到了再更新,点个赞再走啊!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值