Android 基础(二十四) EditText
介紹
A text field allows the user to type text into your app. It can be either single line or multi-line. Touching a text field places the cursor and automatically displays the keyboard. In addition to typing, text fields allow for a variety of other activities, such as text selection (cut, copy, paste) and data look-up via auto-completion.
You can add a text field to you layout with the EditText object. You should usually do so in your XML layout with a < EditText > element.
文本區域給用戶輸入文字提供了方便,它可以是單行的也可以是多行的。觸摸一個文本區域獲取光標并且自動彈出鍵盤。除了可以輸入文字,文本區域還可以用來執行如文本選中(剪切,復制和粘貼)和通過自動補全實現的數據查詢等功能。你可以通過在布局中添加一個EditText對象來實現文本區域,當然也可以在布局文件中添加EditText Tag.
指定鍵盤類型
Text fields can have different input types, such as number, date, password, or email address. The type determines what kind of characters are allowed inside the field, and may prompt the virtual keyboard to optimize its layout for frequently used characters.
You can specify the type of keyboard you want for your EditText object with the android:inputType attribute. For example, if you want the user to input an email address, you should use the textEmailAddress input type:
文本區域有不同的輸入類型,如數字,日期,密碼或者郵箱地址。輸入類型決定了文本區域內允許輸入的字符類型,同時也提示著虛擬鍵盤給用戶展示更加常用的字符集合。我們可以通過使用 android:inputType 屬性來明確我們的輸入框想要的鍵盤類型。例如,如果你想要輸入郵箱地址,你可以使用textEmailAddress輸入類型。
| “none” | 不彈出鍵盤 |
| “text” | 普通文本鍵盤 |
| “textEmailAddress” | 普通文本鍵盤,”@” |
| “textUri” | 普通文本鍵盤,”/” |
| “number” | 數字鍵盤 |
| “phone” | Phone-Style鍵盤 |
控制其他行為
android:inputType屬性不僅可以用來控制顯示特定的鍵盤類型,而且還可以用來明確一些鍵盤的行為,例如是否大寫,自動補全或者是拼寫建議等。android:inputType使用按位與的方式,所以可以指定多種行為
| “textCapSentences” | 正常的文本鍵盤但是每一句話第一個字母大寫 |
| “textCapWords” | 正常文本鍵盤但是每一個單詞的第一個字母大寫 |
| “textAutoCorrect” | 自動提示拼寫錯誤 |
| “textPassword” | 輸入的文字都變成點點 |
| “textMultiLine” | 允許輸入多行文本,可以換行 |
關于android:inputType屬性有很多,上面都只是一些栗子,感覺應該很全了,大家可以自行研究。
指定鍵盤行為
除了改變鍵盤的輸入類型,Android允許當用戶完成輸入指定一個動作,出現的操作指定按鈕的回車鍵和動作,如“搜索”或“發送”鍵。
例如:
鍵盤會顯示出“搜索”按鈕,橫屏之后不僅鍵盤會出現搜索按鈕,右側還會出現對應的字串
主要是對 android:imeOptions 屬性的使用
這么多的行為可供選擇,上面只是舉一個栗子
對鍵盤中定義的行為的響應
通過android:imeOptions 定義的行為,我們可以對它定義的行為作出響應.我們可以使用TextView.OnEditorActionListener來進行事件的監聽和處理。通過如 IME_ACTION_SEND 或者IME_ACTION_SEARCH等事件ID來針對不同的事件行為做出不同的處理。
例如
自定義輸入完成后鍵盤行為的標簽
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="啟動" />自動補全建議
使用AutoCompleteTextView
布局定義
<AutoCompleteTextViewandroid:completionThreshold="1"//默認是2,這就是為什么默認輸入兩個字符才能顯示出提示信息的原因android:id="@+id/autocomplete_country"android:layout_width="match_parent"android:text="@android:string/dialog_alert_title"android:layout_height="wrap_content" />代碼實現
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Get a reference to the AutoCompleteTextView in the layoutAutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);// Get the string arrayString[] countries = getResources().getStringArray(R.array.countries_array);// Create the adapter and set it to the AutoCompleteTextViewArrayAdapter<String> adapter =new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);textView.setAdapter(adapter);//設置提示內容}提示數組
<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>備注
內容主要來自于Android官網教程:
https://developer.android.com/guide/topics/ui/controls/text.html
轉載于:https://www.cnblogs.com/lanzhi/p/6467166.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Android 基础(二十四) EditText的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js循环遍历
- 下一篇: 论文笔记之: Deep Metric L