When we create a view with EditText or AutoCompleteTextView, the input focus is automatically moved to the first input box when loading the view. As shown in:
The following is the mail. xml layout file:
- <!--?xml version="1.0" encoding="utf-8"?-->
- <linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
- <button android:id="@+id/Button01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button_text">
- </button>
- <edittext android:hint="@string/hint" android:id="@+id/EditText01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="">
- </edittext>
- <button android:id="@+id/Button02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button_text">
- </button>
- </linearlayout>
-
How can we remove the focus from EditText? The simplest way is to create an invisible) LinearLayout, which removes the focus from EditText.
Modify the mail. xml layout file and add a LinearLayout before EditText, as shown below:
- <!--?xml version="1.0" encoding="utf-8"?-->
- <linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
- <button android:id="@+id/Button01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button_text">
- </button>
- <linearlayout android:focusable="true" android:focusableintouchmode="true" android:layout_height="0px" android:layout_width="0px">
- <edittext android:hint="@string/hint" android:id="@+id/EditText01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="">
- </edittext>
- <button android:id="@+id/Button02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button_text">
- </button>
- </linearlayout>
- </linearlayout>
The final result is as follows:
The focus is removed without any code. Is it the simplest solution?
I hope this article will help you!