zoukankan      html  css  js  c++  java
  • EditText失去焦点时隐藏软键盘

    Fragment里写在Activity中,好象这个最管用:

     //隐藏软键盘
        //在activity里面重写  dispatchTouchEvent
        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                View v = getCurrentFocus();
                if (isShouldHideInput(v, ev)) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    }
                }
                return super.dispatchTouchEvent(ev);
            }
    // 必不可少,否则所有的组件都不会有TouchEvent了
            if (getWindow().superDispatchTouchEvent(ev)) {
                return true;
            }
            return onTouchEvent(ev);
        }
    
    
        /**
         * 是否隐藏键盘
         */
        public static boolean isShouldHideInput(View v, MotionEvent event) {
            if ((v instanceof EditText)) {
                int[] leftTop = {0, 0};
    //获取输入框当前的location位置
                v.getLocationInWindow(leftTop);
                int left = leftTop[0];
                int top = leftTop[1];
                int bottom = top + v.getHeight();
                int right = left + v.getWidth();
                // 点击的是输入框区域,保留点击EditText的事件
                return !(event.getX() > left) || !(event.getX() < right)
                        || !(event.getY() > top) || !(event.getY() < bottom);
            }
            return false;
        }

    重写方法:

     @Override
        public boolean onTouchEvent(MotionEvent event) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText_phone.getWindowToken(), 0);
            imm.hideSoftInputFromWindow(editText_phone.getWindowToken(), 0);
            return super.onTouchEvent(event);
        }
    

     这个更直接,好理解(rootLayout为根布局):

      /*点击键盘外隐藏软键盘*/
            binding.rootLayout.setOnTouchListener(new View.OnTouchListener() {
                @SuppressLint("ClickableViewAccessibility")
                @Override
                public boolean onTouch(View v, MotionEvent event) {
    
                    if(null!= requireActivity().getCurrentFocus()){
    
                        InputMethodManager manager= (InputMethodManager) requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    
                        if(manager!=null){
                            return manager.hideSoftInputFromWindow(requireActivity().getCurrentFocus().getWindowToken(),0);
                        }
                    }
                    return false;
                }
            });
    

      

    以前的是程序员的老板,现在是末路出家的程序员小白。
  • 相关阅读:
    初学Listener
    初学filter
    Servlet开发
    伪随机数生成
    枚举类
    LeetCode74——Search a 2D Matrix
    STL——lower_bound()
    LeetCode198——house robber(不懂dp)
    LeetCode171——Excel Sheet Column Number
    参数传递的三种方式
  • 原文地址:https://www.cnblogs.com/xiaoyao-blog/p/13690884.html
Copyright © 2011-2022 走看看