当用户触摸屏幕上的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>
答案 0 :(得分:0)
将android:clickable="true"
和android:focusableInTouchMode="true"
设置为所有容器解决了问题。