Android中EditText(或TextView)中的InputType型別含義與如何定義
【背景】
經過一些Android中EditText方面的折騰:
然後對於EditText(或TextView)中的InputType的值的含義和型別,以及如何定義,有了個更清晰點的認識。
現在整理如下:
EditText的InputType屬性,可以在程式碼中設定,也可以預先在xml中定義
設定EditText的InputType屬性,最簡單省事的辦法就是在定義EditText的xml中直接設定。
比如:
想要設定一個可編輯的文字框的輸入內容為只能輸入數字,則就可以:
(1)xml中定義InputType為number
<EditText
android:id ="@+id/variableValue"
......
android:inputType="number" />
(2)程式碼中設定InputType為TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_NORMAL
EditText variableValueView = (EditText) variableLayout.findViewById(R.id.variableValue);
int inputType = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL ;
variableValueView.setInputType(inputType);
這樣的話,之後介面中生成的EditText,當點選後要輸入內容的時候,彈出的輸入法,自動變成那種只能輸入數字的小鍵盤型別的了:
另外,附上,正常的普通字串,即:
xml中:
android:inputType="text"
或程式碼中:
someEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
時,顯示出來的輸入法鍵盤的效果:
EditText的InputType屬性對應的xml定義有哪些,以及程式碼中設定的InputType型別有哪些
知道了設定EditText的InputType屬性值,既可以通過xml中定義,也可以在程式碼中設定為InputType的某種值,但是到底這些值有哪些,以及分別對應的含義是啥,則可以參考官網:
中的完整的列表:
Constant |
Value |
Description |
none |
0×00000000 |
There is no content type. The text is not editable. |
textCapCharacters |
0×00001001 |
Can be combined with text and its variations to request capitalization of all characters. Corresponds to TYPE_TEXT_FLAG_CAP_CHARACTERS . |
textCapWords |
0×00002001 |
Can be combined with text and its variations to request capitalization of the first character of every word. Corresponds to TYPE_TEXT_FLAG_CAP_WORDS . |
textCapSentences |
0×00004001 |
Can be combined with text and its variations to request capitalization of the first character of every sentence. Corresponds to TYPE_TEXT_FLAG_CAP_SENTENCES . |
textAutoCorrect |
0×00008001 |
Can be combined with text and its variations to request auto-correction of text being input. Corresponds toTYPE_TEXT_FLAG_AUTO_CORRECT . |
textAutoComplete |
0×00010001 |
Can be combined with text and its variations to specify that this field will be doing its own auto-completion and talking with the input method appropriately. Corresponds to TYPE_TEXT_FLAG_AUTO_COMPLETE . |
textMultiLine |
0×00020001 |
Can be combined with text and its variations to allow multiple lines of text in the field. If this flag is not set, the text field will be constrained to a single line. Corresponds to TYPE_TEXT_FLAG_MULTI_LINE . |
textImeMultiLine |
0×00040001 |
Can be combined with text and its variations to indicate that though the regular text view should not be multiple lines, the IME should provide multiple lines if it can. Corresponds to TYPE_TEXT_FLAG_IME_MULTI_LINE . |
textNoSuggestions |
0×00080001 |
Can be combined with text and its variations to indicate that the IME should not show any dictionary-based word suggestions. Corresponds to TYPE_TEXT_FLAG_NO_SUGGESTIONS . |
textUri |
0×00000011 |
|
textEmailAddress |
0×00000021 |
|
textEmailSubject |
0×00000031 |
|
textShortMessage |
0×00000041 |
|
textLongMessage |
0×00000051 |
|
textPostalAddress |
0×00000071 |
|
textVisiblePassword |
0×00000091 |
|
textWebEditText |
0x000000a1 |
|
textFilter |
0x000000b1 |
|
textPhonetic |
0x000000c1 |
Text that is for phonetic pronunciation, such as a phonetic name field in a contact entry. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PHONETIC . |
textWebEmailAddress |
0x000000d1 |
|
textWebPassword |
0x000000e1 |
|
numberSigned |
0×00001002 |
Can be combined with number and its other options to allow a signed number. Corresponds to TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_SIGNED . |
numberDecimal |
0×00002002 |
Can be combined with number and its other options to allow a decimal (fractional) number. Corresponds toTYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_DECIMAL . |
phone |
0×00000003 |
For entering a phone number. Corresponds to TYPE_CLASS_PHONE . |
如此,就可以自己去在xml或程式碼中,分別試試,每種不同的InputType對應的都是什麼效果了。