How to remove the input focus on EditText in Android Development

Source: Internet
Author: User

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:

 
  1. <!--?xml version="1.0" encoding="utf-8"?--> 
  2. <linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> 
  3.         <button android:id="@+id/Button01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button_text"> 
  4.         </button> 
  5.         <edittext android:hint="@string/hint" android:id="@+id/EditText01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text=""> 
  6.         </edittext> 
  7.         <button android:id="@+id/Button02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button_text"> 
  8.         </button> 
  9. </linearlayout> 
  10.  

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:

 
  1. <!--?xml version="1.0" encoding="utf-8"?--> 
  2. <linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> 
  3.         <button android:id="@+id/Button01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button_text"> 
  4.         </button> 
  5. <linearlayout android:focusable="true" android:focusableintouchmode="true" android:layout_height="0px" android:layout_width="0px"> 
  6.         <edittext android:hint="@string/hint" android:id="@+id/EditText01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text=""> 
  7.         </edittext> 
  8.         <button android:id="@+id/Button02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button_text"> 
  9.         </button> 
  10. </linearlayout> 
  11. </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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.