给当前界面布局文件的父layout设置点击事件(相当于给整个Activity设置点击事件),在事件里进行键盘隐藏
- <
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:id="@+id/linear_layout_container" android:layout_height="match_parent" style="@style/CommonBlurBackgroundStyle" android:clickable="true" android:orientation="vertical" >
然后在onCreate里,添加onClick事件的监听:
-
/*设置点击键盘外的任意地方关闭键盘*/ findViewById(R.id.linear_layout_container).setOnClickListener(this);
-
@Override public void onClick(View v) { switch (v.getId()) { ..... case R.id.linear_layout_container: InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); break;