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

    原博地址:http://www.cnblogs.com/pangguoming/p/android.html

    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事件的监听:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    @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本身区域的点击事件,然后在事件中进行屏蔽。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    @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)方法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    public  boolean isShouldHideInput(View v, MotionEvent event) { 
        if (v != null && (v instanceof EditText)) { 
            int[] leftTop = { 00 }; 
            //获取输入框当前的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中的事件分发机制是类似,对于处理隐藏事件比较清晰,通过层层事件分发,然后判断是否在需要屏蔽的区域。

  • 相关阅读:
    Control Group(CGroup)资源限制组
    系统安全之用户认证
    如何在Linux中禁用和挂起休眠
    ubuntu18 virtualbox启动失败Kernel driver not installed (rc=-1908)
    C# Winform 多线程更新界面UI控件,解决界面卡顿问题(转)
    【573】LaTeX相关技巧
    程序计时函数
    王炸!!Spring 终于对 JVM 动手了…
    ThreadLocalRandom 是线程安全的吗?
    Spring Boot 应用可视化监控,一目了然!
  • 原文地址:https://www.cnblogs.com/cbx17v/p/6832673.html
Copyright © 2011-2022 走看看