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事件的监听:
findviewbyid(r.id.traceroute_rootview).setonclicklistener(this);
在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中的事件分发机制是类似,对于处理隐藏事件比较清晰,通过层层事件分发,然后判断是否在需要屏蔽的区域。
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事件的监听:
findviewbyid(r.id.traceroute_rootview).setonclicklistener(this);
在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中的事件分发机制是类似,对于处理隐藏事件比较清晰,通过层层事件分发,然后判断是否在需要屏蔽的区域。
以上所述是小编给大家介绍的android点击edittext文本框之外任何地方隐藏键盘的解决办法,希望对大家有所帮助
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
网友评论