网上找了很多例子要么是不加思索拷贝别人的代码或者描述不清楚的。
我这里介绍一个办法
第一
给自己需要隐藏的页面最外层布局命名一个id,如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:screenOrientation="landscape"
android:id="@+id/background_id" >
....
...
....
id为 background_id
第二,在Java代码初始化方法粘贴如下代码
一般卸载oncreate方法比较推荐。
findViewById(R.id.background_id).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
InputMethodManager localInputMethodManager = (InputMethodManager)TicketCheckActivity.this.getSystemService("input_method");
View localView = TicketCheckActivity.this.getCurrentFocus();
if ((localInputMethodManager != null) && (localView != null))
localInputMethodManager.hideSoftInputFromWindow(localView.getWindowToken(), 2);
return false;
}
});
第三,成功啦。