文本框
文本框允許用戶在應用中輸入文本。可以單行輸入也可以多行輸入。點擊文本框會獲得焦點並顯示鍵盤。出了輸入操作之外,文本框還支持其他操作,例如文本選擇(剪切,複製,粘貼)和數據的自動完成。
你可以使用 EditText 對象在佈局中添加一個文本框。不過通常你應該在XML佈局中使用<EditText>元素。
指定鍵盤類型
圖1. 默認text
輸入類型。
圖2. textEmailAddress
輸入類型。
圖3. phone
輸入類型。
文本框有着不同的輸入類型,例如數字,日期,密碼或電子郵件地址。這些類型確定文本框可以輸入什麼類型的字符,並且可能會爲經常使用的字符彈出虛擬鍵盤來優化佈局。
你可以在 EditText 對象上使用 android:inputType
屬性來指定鍵盤類型。例如,如果你想要求用戶輸入電子郵件地址,你應該使用textEmailAddress輸入類型:
<EditText
android:id="@+id/email_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/email_hint"
android:inputType="textEmailAddress" />
針對不同情形有許多不同的輸入類型。下面是 android:inputType
的一些常用值:
"text"
- 標準文本鍵盤。
"textEmailAddress"
- 帶有@字符的標準文本鍵盤。
"textUri"
- 帶有/字符的標準文本鍵盤。
"number"
- 基本數字鍵盤。
"phone"
- 電話樣式鍵盤。
控制其他行爲
android:inputType
還可以讓你指定某些鍵盤行爲,例如是否使所有新單詞的首字母大寫,或使用自動完成和拼寫建議功能。
android:inputType
的屬性還可以按位組合,所以你可以同時指定鍵盤佈局和一個或多個行爲。
這些是定義鍵盤行爲的一些常用輸入類型的值:
"textCapSentences"
- 大寫每個新語句首字母的標準文本鍵盤。
"textCapWords"
- 大寫每個單詞的標準文本鍵盤。適用於標題或人名。
"textAutoCorrect"
- 可以糾正一般拼寫錯誤單詞的標準文本鍵盤。
"textPassword"
- 標準文本鍵盤,但是輸入的字符返回的結果是點。
"textMultiLine"
- 允許用戶輸入包含換行符(回車)的長文本串的標準文本鍵盤。
例如,下面向你介紹如何實現可以輸入郵政地址,每個單詞大寫和禁用文本建議的文本框:
<EditText
android:id="@+id/postal_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/postal_address_hint"
android:inputType="textPostalAddress|
textCapWords|
textNoSuggestions" />
所有行爲都在 android:inputType
文檔中被列出。
指定鍵盤操作
圖4. 如果你聲明瞭android:imeOptions="actionSend"
,那麼這個鍵盤就包含發送操作。
除了改變鍵盤的輸入類型,Android還允許你在用戶完成輸入時指定將要執行的操作。這個操作可以替換回車鍵位置顯示的按鈕和操作,比如“搜索”和“發送”。
你可以通過設置 android:imeOptions
屬性指定操作。例如,這裏介紹如何指定發送行爲:
<EditText
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/search_hint"
android:inputType="text"
android:imeOptions="actionSend" />
如果你沒有明確指明輸入操作,那麼系統會試着確定後面是否有 android:focusable
屬性輸入框。如果後面發現focusable屬性輸入框,系統會爲當前 EditText 應用"actionNext"操作以便用戶可以選擇下一步來移動到下一個輸入框。如果後面沒有focusable屬性輸入框,系統會應用"actionDone"操作。你可以通過設置 android:imeOptions
屬性爲諸如"actionSend"
或"actionSearch"
或默認行爲"actionNone"來重寫操作。
響應操作按鈕事件
如果你已經使用 android:imeOptions
屬性(例如"actionSend")爲輸入法指定了鍵盤的操作,你就可以使用 TextView.OnEditorActionListener 監聽特定的操作事件。TextView.OnEditorActionListener
接口提供一個名爲 onEditorAction()
的回調方法,它通過諸如 IME_ACTION_SEND
或 IME_ACTION_SEARCH
等操作ID調用相關的操作類型方法。
例如,這裏介紹如何監聽用戶點擊鍵盤上的發送按鈕:
EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
});
設置自定義操作按鈕標籤
如果鍵盤太大而不能和放在下面的應用(例如手機橫向時)合理的分配空間,那麼鍵盤就會觸發全屏模式(“提取模式”)。在這種模式下,會在輸入框旁邊顯示一個帶標籤的操作按鈕。你可以通過設置 android:imeActionLabel
屬性來定製這個按鈕的文本:
<EditText
android:id="@+id/launch_codes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/enter_launch_codes"
android:inputType="number"
android:imeActionLabel="@string/launch" />
添加其他鍵盤標誌
你除了可以使用 android:imeOptions
屬性指定操作外,還可以通過添加附加標誌指定其他的鍵盤行爲。在 android:imeOptions
文檔裏連同操作列出了所有可用的標誌。
例如,圖5展示的是系統在手機橫向時如何啓用全屏文本框(或屏幕空間在其他方面被約束使用)。你可以如圖6所示在 android:imeOptions
屬性中設置flagNoExtractUi來禁用全屏輸入模式。
圖6. 使用android:imeOptions="flagNoExtractUi"後全屏文本模式不可用(“提取模式”)。
提供自動完成建議
如果你想在用戶輸入時提供建議,那麼你可以使用 EditText 的名爲 AutoCompleteTextView 的子類達到效果。你必須指定一個能提供文本建議的 Adapter 來實現自動完成功能。有許多種可用的適配器,你可以根據數據來源選擇使用,例如來自數據庫或數組。
圖7. 帶文本建議的 AutoCompleteTextView
事例。
下面的步驟介紹如何設置 AutoCompleteTextView 並使用 ArrayAdapter 從數組提供建議:
- 在佈局中添加
AutoCompleteTextView
。這是個只有文本框的佈局:<?xml version="1.0" encoding="utf-8"?> <AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/autocomplete_country" android:layout_width="fill_parent" android:layout_height="wrap_content" />
- 定義包含文本建議的數組。例如,這是個定義在XML資源文件(res/values/strings.xml)中國家名的數組:
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="countries_array"> <item>Afghanistan</item> <item>Albania</item> <item>Algeria</item> <item>American Samoa</item> <item>Andorra</item> <item>Angola</item> <item>Anguilla</item> <item>Antarctica</item> ... </string-array> </resources>
- 在
Activity
或Fragment
中,使用下面的代碼來指定提供建議的適配器:// Get a reference to the AutoCompleteTextView in the layout AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country); // Get the string array String[] countries = getResources().getStringArray(R.array.countries_array); // Create the adapter and set it to the AutoCompleteTextView ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries); textView.setAdapter(adapter);
在代碼中,實例化一個新的
ArrayAdapter
把COUNTRIES字符數組中的每一項綁定到simple_list_item_1佈局中的TextView上(這是由Android提供的支持列表中文本的標準外觀的佈局)然後調用 setAdapter() 把適配器分配給 AutoCompleteTextView。