EditText not Losing focus on Back Button Press with Scrollview
I've overridden onBackPressed() in my activity like so:
*activity starts with the soft keyboard up, but when the back button is pressed, thereby closing the soft keyboard, the edittext doesn't lose focus, so knowing this, I decided to force the edittext to lose focus manually when the keyboard is closed by pressing the back button
@Override
public void onBackPressed() {
super.onBackPressed();
getCurrentFocus().clearFocus();
}
The problem is that the edittext still doesn't lose focus; even though the root view in my layout has the attributes focusable="true"
and focusableInTouchMode="true"
which seems to be the work around for making sure an edittext doesn't refocus accidentally.
Any idea why my edittext isn't losing focus?
*it does lose focus whenever another edittext gains focus
Here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffffff"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.android.views.CounterEditText
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
custom:maxCharacters="@integer/maxCharactersInTitle">
</com.example.android.views.CounterEditText>
<LinearLayout
android:id="@+id/attributes_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/item_title"
android:orientation="vertical">
<com.example.android.views.CounterEditText
android:id="@+id/attribute_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
custom:maxCharacters="@integer/maxCharactersInAttribute">
</com.example.android.views.CounterEditText>
<com.example.android.views.CounterEditText
android:id="@+id/attribute_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
custom:maxCharacters="@integer/maxCharactersInAttribute">
<com.example.android.views.CounterEditText>
</LinearLayout>
<ImageButton
android:id="@+id/add_attribute_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/attribute_container"
android:background="@null"
android:src="@android:drawable/ic_menu_add"/>
<EditText
android:id="@+id/item_description"
android:layout_width="match_parent"
android:layout_height="125dp"
android:layout_alignParentBottom="true"
android:background="@color/genericGrayBgColor"
android:gravity="center_horizontal"
android:hint="@string/item_description_hint"/>
</RelativeLayout>
</ScrollView>
<Button
android:id="@+id/create_item_button"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/white"
android:text="@string/create_item_button_text"
android:textColor="@android:color/darker_gray"/>
</LinearLayout>
Update:
The problem isn't that getCurrentFocus().clearFocus()
isn't working. The problem is that, when the soft keyboard is up and the back button is pressed, onBackPressed()
isn't called.
上一篇: 如何防止单击按钮时关闭对话框