在Android開發中如何移除EditText上的輸入焦點

來源:互聯網
上載者:User

當我們建立一個帶EditText 或 AutoCompleteTextView的視圖時,在載入視圖時總是會把輸入的焦點自動移動到第一個輸入框。如所示:

下面是mail.xml布局檔案:

 
  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.  

我們如何才能把焦點從EditText上移除呢?最簡單的方法是建立一個不可見的invisible)LinearLayout,LinearLayout將會把焦點從EditText上移走。

我們修改mail.xml布局檔案,在EditText之前增加一個LinearLayout ,如下所示:

 
  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> 

最終效果如下所示:

不需要任何代碼就把焦點移除了,是不是最簡單的解決方案?

希望本文對您有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.