Android 点击空白处自动隐藏输入法,适用于activity与fragment和Dialog

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/InnovationAD/article/details/81215443

点击空白处自动隐藏输入法这个需求很常见,最近也要用到,但是需要处理的页面很零散的几个。看到网上的有很多种方法,但是在fragment上会无效,后来发现一中思路,其实也很简单,就是监听需要处理界面的父布局做处理就可以了。理论上无论是哪里都适用。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:onClick="onClick">


 <EditText
            android:id="@+id/ed_dns_2"
            android:layout_weight="1"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:background="@null"
            android:text=""
            android:textSize="16px"
            android:hint="自动获取"
            android:textColor="@color/colorBlack"
            android:paddingLeft="10px"
            android:paddingRight="10px"
            android:enabled="false"
            />

</RelativeLayout>

onClick处理方法:

private InputMethodManager imm;


 public void onClick(View v) {
                if (null == imm) {
                    imm = (InputMethodManager)         
                      getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                }

                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }

这样就可以了!!!!!如果测试无效的话请留言我改正 谢谢大家

猜你喜欢

转载自blog.csdn.net/InnovationAD/article/details/81215443
今日推荐