1. 程式人生 > >Android系列教程之七:EditText使用詳解-包含很多教程上看不到的功能演示

Android系列教程之七:EditText使用詳解-包含很多教程上看不到的功能演示

Android系列教程目錄:

一:新建HelloEditText工程

新建一個Hello world詳細步驟可以參見

建立設定如下:

  1. Project name:HelloEditText
  2. Build Target :android 2.2
  3. Application name:HelloEditText
  4. Package name:com.flysnow
  5. create Activity:HelloEditText
  6. min SDK 8

 這時候執行還看不到EditText,因為我們還沒有加上,修改main.xml如下:

Xml程式碼  收藏程式碼
  1. <?xmlversion="1.0"encoding="utf-8"
    ?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <EditText
  8.     android:id="@+id/edit_text"
  9.     android:layout_width="fill_parent"
  10.     android:layout_height="wrap_content"
  11.     android:text="這是一個EditText"/>
  12. </LinearLayout>

 這裡添加了一個id為"edit_text"的EditText,設定預設顯示為本為“這是一個EditText”。。執行效果如下:

二:EditText簡介

EditText是一個非常重要的元件,可以說它是使用者和Android應用進行資料傳輸窗戶,有了它就等於有了一扇和Android應用傳輸的門,通過它使用者可以把資料傳給Android應用,然後得到我們想要的資料。

EditText是TextView的子類,所以TextView的方法和特性同樣存在於EditText中,具體的TextView的介紹可以參考上一節

Android系列教程之六:TextView小元件的使用--附帶超連結和跑馬燈效果

三:長度和空白提示文字,提示文字顏色,是否可編輯等

EditText有一些屬性可以設定EditText的特性,比如最大長度,空白提示文字等。

  1. 有時候我們有一些特屬的需要,要求只能在EditText中輸入特定個數的字元,比如身份證號、手機號嗎等。這時候就可以通過android:maxLength屬性來設定最大輸入字元個數,比如android:maxLength=“4”就表示最多能輸入4個字元,再多了就輸入不進去了。
  2. 空白提示文字。有時候我們需要說明你定義的這個EditText是做什麼用的,比如讓輸入“使用者名稱”,或者輸入“電話號碼”等,但是你又不想在EditText前面加一個TextView來說明這是輸入“使用者名稱”的,因為這會使用一個TextView,那麼怎麼辦呢?EditText為我們提供了android:hint來設定當EditText內容為空時顯示的文字,這個文字只在EditText為空時顯示,你輸入字元的時候就消失了,不影響你的EditText的文字。。修改main.xml如下: Xml程式碼  收藏程式碼
    1. <?xmlversion="1.0"encoding="utf-8"?>
    2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     >
    7. <EditText
    8.     android:id="@+id/edit_text"
    9.     android:layout_width="fill_parent"
    10.     android:layout_height="wrap_content"
    11.     android:maxLength="40"
    12.     android:hint="請輸入使用者名稱..."/>
    13. </LinearLayout>
     執行應用就會看到如下的效果: 看看吧,簡潔明瞭還不用新增一個TextView說明,也不影響使用者操作。
  3. 上面列出了空白時的提示文字,有的人說了,我不想要這個灰色的提示文字,和我的應用整體風格不協調,那也行啊,我們可以換顏色,怎麼換呢,就是通過android:textColorHint屬性設定你想要的顏色。修改main.xml如下: Xml程式碼  收藏程式碼
    1. <?xmlversion="1.0"encoding="utf-8"?>
    2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     >
    7. <EditText
    8.     android:id="@+id/edit_text"
    9.     android:layout_width="fill_parent"
    10.     android:layout_height="wrap_content"
    11.     android:maxLength="40"
    12.     android:hint="請輸入使用者名稱..."
    13.     android:textColorHint="#238745"/>
    14. </LinearLayout>
     執行程式效果如下: 看到了吧,顏色已經變了。。
  4. 還有一個比較實用的功能,就是設定EditText的不可編輯。設定android:enabled="false"可以實現不可編輯,可以獲得焦點。這時候我們看到EditText和一個TextView差不多:  
  5. 實現類似html中Textarea的文字域。在Android中沒有專門的文字域元件,但是可以通過設定EditText的高來實現同樣的文字域功能。修改main.xml如下: Xml程式碼  收藏程式碼
    1. <?xmlversion="1.0"encoding="utf-8"?>
    2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     >
    7. <EditText
    8.     android:id="@+id/edit_text"
    9.     android:layout_width="fill_parent"
    10.     android:layout_height="200dip"/>
    11. </LinearLayout>
     執行程式效果如下:  

四:輸入特殊格式的字元

在我們開發程式的時候不免會輸入一些特屬個數的字元,比如密碼(輸入框的字元要加密顯示),電話號碼(比如數字和-),數字等,這些都算是一些特屬格式的字元,強大的EditText同樣為我們提供了輸入這些特屬格式字元的設定。

  1. 密碼文字框。密碼輸入也是Android應用常用的功能,通過配置EditText的android:password="true"就可以實現這一密碼輸入功能,修改main.xml如下: Xml程式碼  收藏程式碼
    1. <?xmlversion="1.0"encoding="utf-8"?>
    2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     >
    7. <EditText
    8.     android:id="@+id/edit_text"
    9.     android:layout_width="fill_parent"
    10.     android:layout_height="wrap_content"
    11.     android:password="true"/>
    12. </LinearLayout>
     執行效果如下: 可以看到我們輸入的字元已經被“.”這樣的掩碼所代替。
  2. 手機中發簡訊打電話是必不可少的,所以用於專門輸入電話號碼的文字框也是大有用途,有了他我們對是否是電話號碼的校驗就容易的多了(因為字元是正確的,只要校驗格式).通過設定android:phoneNumber="true"就可以把EditText變成只接受電話號碼輸入的文字框,連軟鍵盤都已經變成撥號專用軟鍵盤了,所以不用再擔心輸入其他字元了。修改main.xml如下: Xml程式碼  收藏程式碼
    1. <?xmlversion="1.0"encoding="utf-8"?>
    2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     >
    7. <EditText
    8.     android:id="@+id/edit_text"
    9.     android:layout_width="fill_parent"
    10.     android:layout_height="wrap_content"
    11.     android:phoneNumber="true"/>
    12. </LinearLayout>
     執行程式效果如下: 注意看軟鍵盤,已經變成撥號專用的啦.
  3. 有時候我們只想輸入數字,不想輸入字母,EditText為我們提供了android:numeric來控制輸入的數字型別,一共有三種分別為integer(正整數)、signed(帶符號整數)和decimal(浮點數)。這裡以signed型別的為例,修改main.xml如下: Xml程式碼  收藏程式碼
    1. <?xmlversion="1.0"encoding="utf-8"?>
    2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     >
    7. <EditText
    8.     android:id="@+id/edit_text"
    9.     android:layout_width="fill_parent"
    10.     android:layout_height="wrap_content"
    11.     android:numeric="signed"/>
    12. </LinearLayout>
     執行效果如下: 注意這裡的軟鍵盤變成“數字鍵盤”的變化.

五:為文字指定特定的軟鍵盤型別

前面我們通過指定為電話號碼特定格式,然後鍵盤型別變成了撥號專用的鍵盤,這個是自動變的,其實我們也可以通過android:inputType來設定文字的型別,讓輸入法選擇合適的軟鍵盤的。。android:inputType有很多型別,這裡使用date型別來演示,修改main.xml如下:

Xml程式碼  收藏程式碼
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <EditText
  8.     android:id="@+id/edit_text"
  9.     android:layout_width="fill_parent"
  10.     android:layout_height="wrap_content"
  11.     android:inputType="date"/>
  12. </LinearLayout>

 執行效果如下:

六:Enter鍵圖示的設定

軟鍵盤的Enter鍵預設顯示的是“完成”文字,我們知道按Enter建表示前置工作已經準備完畢了,要去什麼什麼啦。比如,在一個搜尋中,我們輸入要搜尋的文字,然後按Enter表示要去搜索了,但是預設的Enter鍵顯示的是“完成”文字,看著不太合適,不符合搜尋的語義,如果能顯示“搜尋”兩個字或者顯示一個表示搜尋的圖示多好。事實證明我們的想法是合理的,Android也為我們提供的這樣的功能。通過設定android:imeOptions來改變預設的“完成”文字。這裡舉幾個常用的常量值:

  1. actionUnspecified  未指定,對應常量EditorInfo.IME_ACTION_UNSPECIFIED.效果:
  2. actionNone 沒有動作,對應常量EditorInfo.IME_ACTION_NONE 效果:
  3. actionGo 去往,對應常量EditorInfo.IME_ACTION_GO 效果:
  4. actionSearch 搜尋,對應常量EditorInfo.IME_ACTION_SEARCH 效果:
  5. actionSend 傳送,對應常量EditorInfo.IME_ACTION_SEND 效果:
  6. actionNext 下一個,對應常量EditorInfo.IME_ACTION_NEXT 效果:
  7. actionDone 完成,對應常量EditorInfo.IME_ACTION_DONE 效果:

 下面已搜尋為例,演示一個例項,修改main.xml如下:

Xml程式碼  收藏程式碼
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <EditText
  8.     android:id="@+id/edit_text"
  9.     android:layout_width="fill_parent"
  10.     android:layout_height="wrap_content"
  11.     android:imeOptions="actionSearch"/>
  12. </LinearLayout>

  修改HelloEditText如下:

Java程式碼  收藏程式碼
  1. package com.flysnow;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.KeyEvent;  
  5. import android.widget.EditText;  
  6. import android.widget.TextView;  
  7. import android.widget.Toast;  
  8. import android.widget.TextView.OnEditorActionListener;  
  9. publicclass HelloEditText extends Activity {  
  10.     /** Called when the activity is first created. */
  11.     @Override
  12.     publicvoid onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.main);  
  15.         EditText editText=(EditText)findViewById(R.id.edit_text);  
  16.         editText.setOnEditorActionListener(new OnEditorActionListener() {  
  17.             @Override
  18.             publicboolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  19.                 Toast.makeText(HelloEditText.this, String.valueOf(actionId), Toast.LENGTH_SHORT).show();  
  20.                 returnfalse;  
  21.             }  
  22.         });  
  23.     }  
  24. }  

 執行程式,點選回車(也就是搜尋圖示軟鍵盤按鈕)會顯示該actionId.我們上面的每一個設定都會對應一個常量,這裡的actionId就是那個常量值。

七:EditText的取值、全選、部分選擇、獲取選中文字

       下面通過一個例子來演示EditText的取值、全選、部分選擇和獲取選中文字.main.xml修改如下:

Xml程式碼  收藏程式碼
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <EditText
  8.     android:id="@+id/edit_text"
  9.     android:layout_width="fill_parent"
  10.     android:layout_height="wrap_content"
  11.     android:imeOptions="actionSearch"/>
  12. <Button
  13.     android:id="@+id/btn_get_value"
  14.     android:text="取值"
  15.     android:layout_width="wrap_content"
  16.     android:layout_height="wrap_content"/>
  17. <Button
  18.     android:id="@+id/btn_all"
  19.     android:text="全選"
  20.     android:layout_width="wrap_content"
  21.     android:layout_height="wrap_content"/>
  22. <Button
  23.     android:id="@+id/btn_select"
  24.     android:text="從第2個字元開始選擇"
  25.     android:layout_width="wrap_content"
  26.     android:layout_height="wrap_content"/>
  27. <Button
  28.     android:id="@+id/btn_get_select"
  29.     android:text="獲取選中文字"
  30.     android:layout_width="wrap_content"
  31.     android:layout_height="wrap_content"/>
  32. </LinearLayout>
 

HelloEditText修改如下:

Java程式碼  收藏程式碼
  1. package com.flysnow;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.text.Editable;  
  5. import android.text.Selection;  
  6. import android.view.KeyEvent;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import android.widget.TextView;  
  12. import android.widget.Toast;  
  13. import android.widget.TextView.OnEditorActionListener;  
  14. /** 
  15.  * EditText演示 
  16.  * @author 飛雪無情 
  17.  * @since 2010-11-29 
  18.  */
  19. publicclass HelloEditText extends Activity {  
  20.     /** Called when the activity is first created. */
  21.     @Override
  22.     publicvoid onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.         final EditText editText=(EditText)findViewById(R.id.edit_text);  
  26.         //監聽回車鍵
  27.         editText.setOnEditorActionListener(new OnEditorActionListener() {  
  28.             @Override
  29.             publicboolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  30.                 Toast.makeText(HelloEditText.this, String.valueOf(actionId), Toast.LENGTH_SHORT).show();  
  31.                 returnfalse;  
  32.             }  
  33.         });  
  34.         //獲取EditText文字
  35.         Button getValue=(Button)findViewById(R.id.btn_get_value);  
  36.         getValue.setOnClickListener(new OnClickListener() {  
  37.             @Override
  38.             publicvoid onClick(View v) {  
  39.                 Toast.makeText(HelloEditText.this, editText.getText().toString(), Toast.LENGTH_SHORT).show();  
  40.             }  
  41.         });  
  42.         //讓EditText全選
  43.         Button all=(Button)findViewById(R.id.btn_all);  
  44.         all.setOnClickListener(new OnClickListener() {  
  45.             @Override
  46.             publicvoid onClick(View v) {  
  47.                 editText.selectAll();  
  48.             }  
  49.         });  
  50.         //從第2個字元開始選擇EditText文字
  51.         Button select=(Button)findViewById(R.id.btn_select);  
  52.         select.setOnClickListener(new OnClickListener() {  
  53.             @Override
  54.             publicvoid onClick(View v) {  
  55.                 Editable editable=editText.getText();  
  56.                 Selection.setSelection(editable, 1,editable.length());  
  57.             }  
  58.         });  
  59.       //獲取選中的文字
  60.         Button getSelect=(Button)findViewById(R.id.btn_get_select);  
  61.         getSelect.setOnClickListener(new OnClickListener() {  
  62.             @Override
  63.             publicvoid onClick(View v) {  
  64.                 int start=editText.getSelectionStart();  
  65.                 int end=editText.getSelectionEnd();  
  66.                 CharSequence selectText=editText.getText().subSequence(start, end);  
  67.                 Toast.makeText(HelloEditText.this, selectText, Toast.LENGTH_SHORT).show();  
  68.             }  
  69.         });  
  70.     }  
  71.     /** 
  72.      * 交換兩個索引 
  73.      * @param start 開始索引 
  74.      * @param end 結束索引 
  75.      */
  76.     protectedvoid switchIndex(int start, int end) {  
  77.         int temp=start;  
  78.         start=end;  
  79.         end=temp;  
  80.     }  
  81. }  
 

 執行效果如下:

 可以通過輸入文字和點選下面的按鈕測試。

八:小結

     這結詳細介紹了EditText的大部分特性和常用功能,如常用的密碼框,獲取值等等。這幾天忙的沒更新,這次更新個長的。可以夠消化一陣子的。

轉自:http://android.group.iteye.com/group/wiki/2910-android-widget-edittext