zoukankan      html  css  js  c++  java
  • Android点击EditText文本框之外任何地方隐藏键盘的解决办法

    1,实现方法一:通过给当前界面布局文件的父layout设置点击事件(相当于给整个Activity设置点击事件),在事件里进行键盘隐藏

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:id="@+id/traceroute_rootview"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:background="@color/white"  
        android:clickable="true"  
        android:gravity="center_horizontal"  
        android:orientation="vertical" >  
      
    </LinearLayout> 
    

      加上id和clickable=true

    然后在onCreate里,添加onClick事件的监听:

    @Override  
    public void onClick(View v) {  
        switch (v.getId()) {  
        case R.id.traceroute_rootview:  
             InputMethodManager imm = (InputMethodManager)  
             getSystemService(Context.INPUT_METHOD_SERVICE);  
             imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  
            break;  
        }  
      
    }  
    

      

    这样就可以完美的解决了输入框外的隐藏效果,对于布局不是特别复杂或是其它触摸事件少的情况下可以使用。

    2,实现思路二:通过dispatchTouchEvent每次ACTION_DOWN事件中动态判断非EditText本身区域的点击事件,然后在事件中进行屏蔽。

    @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);  
    }  
    

      

    isShoudHideInput(View v,MotionEvent e)方法:

    public  boolean isShouldHideInput(View v, MotionEvent event) {  
        if (v != null && (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();  
            if (event.getX() > left && event.getX() < right  
                    && event.getY() > top && event.getY() < bottom) {  
                // 点击的是输入框区域,保留点击EditText的事件  
                return false;  
            } else {  
                return true;  
            }  
        }  
        return false;  
    }  
    

      这种方法实现起来比较麻烦,解决思路与iOS中的事件分发机制是类似,对于处理隐藏事件比较清晰,通过层层事件分发,然后判断是否在需要屏蔽的区域。

     

  • 相关阅读:
    leetcode 122. Best Time to Buy and Sell Stock II
    leetcode 121. Best Time to Buy and Sell Stock
    python 集合(set)和字典(dictionary)的用法解析
    leetcode 53. Maximum Subarray
    leetcode 202. Happy Number
    leetcode 136.Single Number
    leetcode 703. Kth Largest Element in a Stream & c++ priority_queue & minHeap/maxHeap
    [leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree
    正则表达式
    十种排序算法
  • 原文地址:https://www.cnblogs.com/pangguoming/p/android.html
Copyright © 2011-2022 走看看