RecycleView (通过LinearLayoutManager)显示软键盘,点击外部隐藏软键盘

1 fragment 中的布局使用RecycleView才会使软键盘将View顶上去(外部是FramLayout容器)

 

            
        

    

  //这里是一个Edittext+TextView(发送)  下面是里边的内容

    



    

    

2 activity不需要什么设置

3 进入正题 显示软键盘

InputMethodManager imm = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
private void showInput(String hint) {
        rlImport.setVisibility(View.VISIBLE);
        editText.requestFocus();
        editText.setFocusable(true);
        editText.setHint(hint);
        imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
        //  imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
    }

4 调整 RecycleView的点击条目底部刚好是Edittext容器的顶部

 linearLayoutManager = new LinearLayoutManager(activity);
                countrySideAdapter = new CountrySideAdapter(rowsBeans);
                rvView.setLayoutManager(linearLayoutManager);

 //评论
    private void comment(int position) {
         showInput("请输入内容");
         final View itemCircle = linearLayoutManager.findViewByPosition(position);
         new Handler().postDelayed(() -> {
             int scrollLocation = getScrollLocation(itemCircle);
             rvView.smoothScrollBy(0, scrollLocation);
         }, 400);
    }

   public int getScrollLocation(View itemView) {
        int[] importLocation = new int[2];
        rlImport.getLocationOnScreen(importLocation);

        itemView.getHeight();
        int[] itemViewLocation = new int[2];
        itemView.getLocationOnScreen(itemViewLocation);
        return (itemViewLocation[1] + itemView.getHeight()) - importLocation[1];
    }

5 点击外部(软键盘+底部rlImport之外的部分)隐藏软键盘

public void hideInputWindow(Activity context) {
        if (editText != null && editText.getWindowToken() != null) {
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
        rlImport.setVisibility(View.INVISIBLE);
    }

activity中监听分发事件

 @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                View view = getCurrentFocus();
                if (isShouldHideInput(view, ev)) {
                    /*InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                    if (imm.isActive()) {
                        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                    }*/
                    Message msg = Message.obtain();
                    msg.what = Constans.HIDE_INPUT;
                    EventBus.getDefault().post(msg);
                }
                break;
        }
        return super.dispatchTouchEvent(ev);

    }

 private boolean isShouldHideInput(View view, MotionEvent ev) {
        // if (view != null && view.getId() == R.id.rl_import) instanceof EditText) {
        if (view != null && view.getId() == R.id.dynamicstate_comments_EditText) {
            int[] locationXY = new int[2];
            view.getLocationInWindow(locationXY);
            int leftX = locationXY[0];
            int rightX = locationXY[0] + view.getWidth();
            int topY = locationXY[1];
            int bottomY = locationXY[1] + view.getHeight();
            float downX = ev.getX();
            float downY = ev.getY();
            if (downY >= topY && downY <= bottomY) {
                return false;
            }
            return true;
        } else {
            return false;
        }
    }

//frament中接受事件(是显示的情况下进行隐藏)

 @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Message message) {
        switch (message.what) {
            case Constans.HIDE_INPUT:
                if (getUserVisibleHint()) {
                    hideInputWindow(activity);
                }
                break;
        }
    }

 

你可能感兴趣的:(全软键盘)