在 Android 上隐藏软键盘不适用于 onFocusChangeListener
Hiding soft keyboard not working with onFocusChangeListener on Android
我希望当用户触摸屏幕上除 EditText 以外的任何内容时软键盘消失。我之前已经浏览过关于这个问题的 SO 帖子,并实现了 onFocusChangeListener 的使用,它在我从事的其他项目中起到了作用,但它现在似乎不起作用,我不知道出了什么问题。感谢任何帮助。
我的activity.java(inputGuardian1是EditText的例子):
inputGuardian1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(inputGuardian1.getWindowToken(), 0);
}
}
});
布局xml文件:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp" >
<!--android:layout_marginTop="?attr/actionBarSize"-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_guardian1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/inputGuardian1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="Guardian 1" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_Guardian2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/inputGuardian2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="Guardian 2" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_Guardian3"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/inputGuardian3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="Guardian 3" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_Guardian4"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/inputGuardian4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="Guardian 4" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="40sp"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_ChildName"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/inputChildName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="Child's Name"
android:layout_marginTop="30sp"
android:imeOptions="actionDone"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<Button android:id="@+id/buttonNext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next"
android:background="@color/colorPrimary"
android:layout_marginTop="40dp"
android:textColor="@android:color/white" />
</LinearLayout>
</ScrollView>
为所有容器设置 android:clickable="true"
和 android:focusableInTouchMode="true"
解决了问题。
我希望当用户触摸屏幕上除 EditText 以外的任何内容时软键盘消失。我之前已经浏览过关于这个问题的 SO 帖子,并实现了 onFocusChangeListener 的使用,它在我从事的其他项目中起到了作用,但它现在似乎不起作用,我不知道出了什么问题。感谢任何帮助。
我的activity.java(inputGuardian1是EditText的例子):
inputGuardian1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(inputGuardian1.getWindowToken(), 0);
}
}
});
布局xml文件:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp" >
<!--android:layout_marginTop="?attr/actionBarSize"-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_guardian1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/inputGuardian1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="Guardian 1" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_Guardian2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/inputGuardian2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="Guardian 2" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_Guardian3"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/inputGuardian3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="Guardian 3" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_Guardian4"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/inputGuardian4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="Guardian 4" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="40sp"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_ChildName"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/inputChildName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="Child's Name"
android:layout_marginTop="30sp"
android:imeOptions="actionDone"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<Button android:id="@+id/buttonNext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next"
android:background="@color/colorPrimary"
android:layout_marginTop="40dp"
android:textColor="@android:color/white" />
</LinearLayout>
</ScrollView>
为所有容器设置 android:clickable="true"
和 android:focusableInTouchMode="true"
解决了问题。