android:inputType

看到了就記一記啊

@[toc]

前言

android:inputType 是開發(fā)中非常常用的字段了,但你知道他都有哪些取值嗎?
先一起看一下吧,然后我們逐個說一下。

inputType

首先,要避免一個誤區(qū),這個是 輸入類型,而不是顯示類型,
所以在有些類型的解釋上可能要注意這個問題。它更多的是影響了鍵盤或者其它識別的輸入問題,比如候選項,比如實時的糾錯和補全,或者其它數(shù)值問題。
當(dāng)然,不可避免的它從輸入的角度去限制了一部分展示的問題。

inputType設(shè)置有兩種方式。
一種在代碼中通過 setInputType(InputType.TYPE_CLASS_TEXT)
另一種非常常用了,xml布局中使用:android:inputType="text"

下面先列舉我們在xml布局中能用的 inputType 都有哪些類型,稍后逐一看一下。

--> platforms/android-26/data/res/values/attres.xml#inputType

 <!-- The type of data being placed in a text field, used to help an
         input method decide how to let the user enter text.  The constants
         here correspond to those defined by
         {@link android.text.InputType}.  Generally you can select
         a single value, though some can be combined together as
         indicated.  Setting this attribute to anything besides
         <var>none</var> also implies that the text is editable. -->
    <attr name="inputType">
        <!-- There is no content type.  The text is not editable. -->
        <flag name="none" value="0x00000000" />

        <!-- Just plain old text.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_TEXT} |
             {@link android.text.InputType#TYPE_TEXT_VARIATION_NORMAL}. -->
        <flag name="text" value="0x00000001" />

        <!-- Can be combined with <var>text</var> and its variations to
             request capitalization of all characters.  Corresponds to
             {@link android.text.InputType#TYPE_TEXT_FLAG_CAP_CHARACTERS}. -->
        <flag name="textCapCharacters" value="0x00001001" />

        <!-- Can be combined with <var>text</var> and its variations to
             request capitalization of the first character of every word.  Corresponds to
             {@link android.text.InputType#TYPE_TEXT_FLAG_CAP_WORDS}. -->
        <flag name="textCapWords" value="0x00002001" />

        <!-- Can be combined with <var>text</var> and its variations to
             request capitalization of the first character of every sentence.  Corresponds to
             {@link android.text.InputType#TYPE_TEXT_FLAG_CAP_SENTENCES}. -->
        <flag name="textCapSentences" value="0x00004001" />

        <!-- Can be combined with <var>text</var> and its variations to
             request auto-correction of text being input.  Corresponds to
             {@link android.text.InputType#TYPE_TEXT_FLAG_AUTO_CORRECT}. -->
        <flag name="textAutoCorrect" value="0x00008001" />

        <!-- Can be combined with <var>text</var> and its variations to
             specify that this field will be doing its own auto-completion and
             talking with the input method appropriately.  Corresponds to
             {@link android.text.InputType#TYPE_TEXT_FLAG_AUTO_COMPLETE}. -->
        <flag name="textAutoComplete" value="0x00010001" />

        <!-- Can be combined with <var>text</var> 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
             {@link android.text.InputType#TYPE_TEXT_FLAG_MULTI_LINE}. -->
        <flag name="textMultiLine" value="0x00020001" />

        <!-- Can be combined with <var>text</var> 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
             {@link android.text.InputType#TYPE_TEXT_FLAG_IME_MULTI_LINE}. -->
        <flag name="textImeMultiLine" value="0x00040001" />

        <!-- Can be combined with <var>text</var> and its variations to
             indicate that the IME should not show any
             dictionary-based word suggestions.  Corresponds to
             {@link android.text.InputType#TYPE_TEXT_FLAG_NO_SUGGESTIONS}. -->
        <flag name="textNoSuggestions" value="0x00080001" />

        <!-- Text that will be used as a URI.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_TEXT} |
             {@link android.text.InputType#TYPE_TEXT_VARIATION_URI}. -->
        <flag name="textUri" value="0x00000011" />

        <!-- Text that will be used as an e-mail address.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_TEXT} |
             {@link android.text.InputType#TYPE_TEXT_VARIATION_EMAIL_ADDRESS}. -->
        <flag name="textEmailAddress" value="0x00000021" />

        <!-- Text that is being supplied as the subject of an e-mail.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_TEXT} |
             {@link android.text.InputType#TYPE_TEXT_VARIATION_EMAIL_SUBJECT}. -->
        <flag name="textEmailSubject" value="0x00000031" />

        <!-- Text that is the content of a short message.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_TEXT} |
             {@link android.text.InputType#TYPE_TEXT_VARIATION_SHORT_MESSAGE}. -->
        <flag name="textShortMessage" value="0x00000041" />

        <!-- Text that is the content of a long message.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_TEXT} |
             {@link android.text.InputType#TYPE_TEXT_VARIATION_LONG_MESSAGE}. -->
        <flag name="textLongMessage" value="0x00000051" />

        <!-- Text that is the name of a person.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_TEXT} |
             {@link android.text.InputType#TYPE_TEXT_VARIATION_PERSON_NAME}. -->
        <flag name="textPersonName" value="0x00000061" />

        <!-- Text that is being supplied as a postal mailing address.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_TEXT} |
             {@link android.text.InputType#TYPE_TEXT_VARIATION_POSTAL_ADDRESS}. -->
        <flag name="textPostalAddress" value="0x00000071" />

        <!-- Text that is a password.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_TEXT} |
             {@link android.text.InputType#TYPE_TEXT_VARIATION_PASSWORD}. -->
        <flag name="textPassword" value="0x00000081" />

        <!-- Text that is a password that should be visible.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_TEXT} |
             {@link android.text.InputType#TYPE_TEXT_VARIATION_VISIBLE_PASSWORD}. -->
        <flag name="textVisiblePassword" value="0x00000091" />

        <!-- Text that is being supplied as text in a web form.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_TEXT} |
             {@link android.text.InputType#TYPE_TEXT_VARIATION_WEB_EDIT_TEXT}. -->
        <flag name="textWebEditText" value="0x000000a1" />

        <!-- Text that is filtering some other data.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_TEXT} |
             {@link android.text.InputType#TYPE_TEXT_VARIATION_FILTER}. -->
        <flag name="textFilter" value="0x000000b1" />

        <!-- Text that is for phonetic pronunciation, such as a phonetic name
             field in a contact entry.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_TEXT} |
             {@link android.text.InputType#TYPE_TEXT_VARIATION_PHONETIC}. -->
        <flag name="textPhonetic" value="0x000000c1" />

        <!-- Text that will be used as an e-mail address on a web form.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_TEXT} |
             {@link android.text.InputType#TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS}. -->
        <flag name="textWebEmailAddress" value="0x000000d1" />

        <!-- Text that will be used as a password on a web form.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_TEXT} |
             {@link android.text.InputType#TYPE_TEXT_VARIATION_WEB_PASSWORD}. -->
        <flag name="textWebPassword" value="0x000000e1" />

        <!-- A numeric only field.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_NUMBER} |
             {@link android.text.InputType#TYPE_NUMBER_VARIATION_NORMAL}. -->
        <flag name="number" value="0x00000002" />

        <!-- Can be combined with <var>number</var> and its other options to
             allow a signed number.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_NUMBER} |
             {@link android.text.InputType#TYPE_NUMBER_FLAG_SIGNED}. -->
        <flag name="numberSigned" value="0x00001002" />

        <!-- Can be combined with <var>number</var> and its other options to
             allow a decimal (fractional) number.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_NUMBER} |
             {@link android.text.InputType#TYPE_NUMBER_FLAG_DECIMAL}. -->
        <flag name="numberDecimal" value="0x00002002" />

        <!-- A numeric password field.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_NUMBER} |
             {@link android.text.InputType#TYPE_NUMBER_VARIATION_PASSWORD}. -->
        <flag name="numberPassword" value="0x00000012" />

        <!-- For entering a phone number.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_PHONE}. -->
        <flag name="phone" value="0x00000003" />

        <!-- For entering a date and time.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_DATETIME} |
             {@link android.text.InputType#TYPE_DATETIME_VARIATION_NORMAL}. -->
        <flag name="datetime" value="0x00000004" />

        <!-- For entering a date.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_DATETIME} |
             {@link android.text.InputType#TYPE_DATETIME_VARIATION_DATE}. -->
        <flag name="date" value="0x00000014" />

        <!-- For entering a time.  Corresponds to
             {@link android.text.InputType#TYPE_CLASS_DATETIME} |
             {@link android.text.InputType#TYPE_DATETIME_VARIATION_TIME}. -->
        <flag name="time" value="0x00000024" />
    </attr>

"none" 無限制類型

android:inputType="none"
There is no content type. The text is not editable.
沒有內(nèi)容類型。文本不可編輯。

翻譯過來,是這么說的,但是。。。
這個類型還真可以編輯。。。
其實可以理解為普通無類型文本,不會出現(xiàn)什么候選處理
從輸入類型的角度去看,可能就比較好理解了,輸入法或者鍵盤鍵入不認為這是可以編輯的文本,就不會對這個文本做特別的處理。
也就是可能某種程度上會限制部分輸入方式,比如不會提供候選內(nèi)容,
比如,語音輸入的限制,手寫限制,或者其它識別情況

對應(yīng) InputType.TYPE_NULL

android:inputType="none"

editView.setInputType(InputType.TYPE_NULL)

"text" 普通文本類型

咱們最常用的,普通文本類型。
這個也啥好說的。

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL

android:inputType="text"

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL)

"textCapCharacters" 全部字符大寫

輸入的每個字符都默認大寫。
只對有單詞大小寫的文字有影響,比如,中文他就影響不了,哈哈

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS

android:inputType="textCapCharacters"

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS)

"textCapWords" 單詞首字母大寫

每個單詞的首字母大寫,其它都是小寫。
只對有單詞大小寫的文字有影響,中文他也影響不了

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS

android:inputType="textCapWords"

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS)

"textCapSentences" 句子首字母大寫

每個句子的第一個字符大寫,其它都是小寫。
只對有大小寫的文字有影響,中文他還是影響不了

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES

android:inputType="textCapSentences"

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES)

"textAutoCorrect" 自動修正

會自動幫你修訂拼錯的單詞,是這么解釋的,但是貌似也是中文無效。
場景:比如聊天吧,哈

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT

android:inputType="textAutoCorrect"

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT)

"textAutoComplete" 自動補全

文本編輯器(即應(yīng)用程序)根據(jù)其自身的語義自動完成輸入的文本,并在用戶鍵入文本時將其呈現(xiàn)給用戶。這通常意味著輸入法不應(yīng)該顯示候選項本身,但可以期望編輯器從InputMethodSession提供自己的補全/候選項。displayCompletions()是編輯器調(diào)用InputMethodManager的結(jié)果。請注意,與TYPE_TEXT_FLAG_AUTO_CORRECT和TYPE_TEXT_FLAG_NO_SUGGESTIONS相比,TYPE_TEXT_FLAG_AUTO_COMPLETE意味著編輯器應(yīng)該顯示一個顯示建議的界面,但它不提供自己的界面,而是依賴編輯器來傳遞完成/更正。

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE

android:inputType="textAutoComplete"

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE)

"textMultiLine" 多行輸入

允許輸入多行。
比較明顯的變化就是彈出的輸入法會包含enter換行鍵~
如果沒有這個設(shè)置,一般不太會出現(xiàn)換行鍵。
這個可能就是很多app中聊天支持的問題根源~

注意:如果未設(shè)置此標志,并且文本字段沒有最大長度限制,出于性能原因,框架會自動將字符的最大長度設(shè)置為5000。

這個提示是源碼中的,我沒試過這么大長度的,但是個人理解的可能是輸入法自身的長度限制。

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE

android:inputType="textMultiLine"

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE)

"textImeMultiLine" 輸入法多行輸入

與此關(guān)聯(lián)的常規(guī)文本視圖不應(yīng)該是多行的,但是當(dāng)全屏輸入法提供文本時,如果可以,它應(yīng)該使用多行。

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE

android:inputType="textImeMultiLine"

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE)

"textNoSuggestions" 無提示候選信息

輸入法不需要顯示任何基于字典的候選項。這對于不包含該語言中的單詞且不受益于任何基于詞典的補全或更正的文本視圖非常有用。

設(shè)置之后,直接展示你所輸入的內(nèi)容,不做任何關(guān)聯(lián)性提示

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS

android:inputType="textNoSuggestions" 

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS)

"textUri" uri格式輸入

輸入為Uri格式文本

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI

android:inputType="textUri" 

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI)

"textEmailAddress" 郵件地址格式

輸入為e-mail郵件地址。

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS

android:inputType="textEmailAddress" 

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS)

"textEmailSubject" 郵件主題

輸入為e-mail郵件的標題主題。

有點像郵件類app的需求。
也可能是Android應(yīng)用于自身郵件的設(shè)置。

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_SUBJECT

android:inputType="textEmailSubject" 

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_SUBJECT)

"textShortMessage" 短消息信息模式

短消息信息模式。
內(nèi)容比較少,類似短信啊,即時聊天啥的。

也可能是Android應(yīng)用于自身短信的設(shè)置。

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE

android:inputType="textShortMessage" 

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE)

"textLongMessage" 長消息信息模式

長消息信息模式
跟短相對吧,哈哈
比較正式的長消息內(nèi)容,如郵件的正式內(nèi)容等。

也可能是Android應(yīng)用于自身郵件正文的設(shè)置。

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_LONG_MESSAGE

android:inputType="textLongMessage" 

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_LONG_MESSAGE)

"textPersonName" 人名輸入

外國人的那種人名。

可能是用于Android通訊錄中的模式吧。

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PERSON_NAME

android:inputType="textPersonName" 

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PERSON_NAME)

"textPostalAddress" 郵寄地址

我有點沒分清
不清楚是實際地理地址,還是類似于咱們的那種郵編
我個人傾向前者

也可能是用于Android通訊錄中的模式吧。

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS

android:inputType="textPostalAddress" 

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS)

"textPassword" 密碼格式

應(yīng)該也算是常用格式之一了,輸入之后會變成星號不可見

常用于用戶密碼輸入。

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD

android:inputType="textPassword" 

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)

"textVisiblePassword" 密碼可見格式

密碼對用戶可見

常用于用戶密碼輸入。

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD

android:inputType="textVisiblePassword" 

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD)

"textWebEditText" web表單格式

輸入內(nèi)容為web表單格式

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT

android:inputType="textWebEditText" 

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT)

"textFilter" 文本篩選

輸入文本用于列表的內(nèi)容篩選等功能

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_FILTER

android:inputType="textFilter" 

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_FILTER)

"textPhonetic" 拼音輸入

輸入為拼音內(nèi)容
當(dāng)然針對的是帶拼音的語言,不僅僅是中文拼音

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PHONETIC

android:inputType="textPhonetic" 

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PHONETIC)

"textWebEmailAddress" web表單中添加郵件地址

和郵件地址模式一樣
不過,用于web表單的輸入

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS

android:inputType="textWebEmailAddress" 

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS)

"textWebPassword" web表單中添加密碼

和密碼模式一樣
不過,用于web表單的輸入

對應(yīng)了 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD

android:inputType="textWebPassword" 

editView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD)

"number" 數(shù)字格式

輸入內(nèi)容為數(shù)字,默認打開數(shù)字鍵盤

對應(yīng)了 InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL

android:inputType="number" 

editView.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL)

"numberSigned" 有符號數(shù)字格式

輸入內(nèi)容為數(shù)字,可以輸入正負號

默認打開數(shù)字鍵盤

對應(yīng)了 InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED

android:inputType="numberSigned" 

editView.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED)

"numberDecimal" 浮點數(shù)字格式

輸入內(nèi)容為數(shù)字,可以輸入小數(shù)點了

默認打開數(shù)字鍵盤

對應(yīng)了 InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL

android:inputType="numberDecimal" 

editView.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL)

"numberPassword" 純數(shù)字密碼格式

輸入內(nèi)容為數(shù)字格式的密碼

默認打開數(shù)字鍵盤

對應(yīng)了 InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD

android:inputType="numberPassword" 

editView.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD)

"phone" 電話號碼模式

輸入內(nèi)容為電話號碼

應(yīng)該是可以輸入數(shù)字加空格和橫杠吧

稍后會嘗試一下

默認打開數(shù)字鍵盤

對應(yīng)了 InputType.TYPE_CLASS_PHONE

android:inputType="phone" 

editView.setInputType(InputType.TYPE_CLASS_PHONE)

"datetime" 時間日期格式

即允許輸入時間也可以輸入日期

對應(yīng)了 InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_NORMAL

android:inputType="datetime" 

editView.setInputType(InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_NORMAL)

"date" 日期鍵盤

只允許輸入日期格式

對應(yīng)了 InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_DATE

android:inputType="date" 

editView.setInputType(InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_DATE)

"time" 時間鍵盤

只允許輸入時間格式

對應(yīng)了 InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_TIME

android:inputType="time" 

editView.setInputType(InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_TIME)

附錄一: android.text.InputType


package android.text;

/**
 * Bit definitions for an integer defining the basic content type of text
 * held in an {@link Editable} object. Supported classes may be combined
 * with variations and flags to indicate desired behaviors.
 *
 * <h3>Examples</h3>
 *
 * <dl>
 * <dt>A password field with with the password visible to the user:
 * <dd>inputType = TYPE_CLASS_TEXT |
 *     TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
 *
 * <dt>A multi-line postal address with automatic capitalization:
 * <dd>inputType = TYPE_CLASS_TEXT |
 *     TYPE_TEXT_VARIATION_POSTAL_ADDRESS |
 *     TYPE_TEXT_FLAG_MULTI_LINE
 *
 * <dt>A time field:
 * <dd>inputType = TYPE_CLASS_DATETIME |
 *     TYPE_DATETIME_VARIATION_TIME
 * </dl>
 */
public interface InputType {
    /**
     * Mask of bits that determine the overall class
     * of text being given.  Currently supported classes are:
     * {@link #TYPE_CLASS_TEXT}, {@link #TYPE_CLASS_NUMBER},
     * {@link #TYPE_CLASS_PHONE}, {@link #TYPE_CLASS_DATETIME}.
     * <p>IME authors: If the class is not one you
     * understand, assume {@link #TYPE_CLASS_TEXT} with NO variation
     * or flags.<p>
     */
    public static final int TYPE_MASK_CLASS = 0x0000000f;

    /**
     * Mask of bits that determine the variation of
     * the base content class.
     */
    public static final int TYPE_MASK_VARIATION = 0x00000ff0;

    /**
     * Mask of bits that provide addition bit flags
     * of options.
     */
    public static final int TYPE_MASK_FLAGS = 0x00fff000;

    /**
     * Special content type for when no explicit type has been specified.
     * This should be interpreted to mean that the target input connection
     * is not rich, it can not process and show things like candidate text nor
     * retrieve the current text, so the input method will need to run in a
     * limited "generate key events" mode, if it supports it. Note that some
     * input methods may not support it, for example a voice-based input
     * method will likely not be able to generate key events even if this
     * flag is set.
     */
    public static final int TYPE_NULL = 0x00000000;

    // ----------------------------------------------------------------------
    // ----------------------------------------------------------------------
    // ----------------------------------------------------------------------

    /**
     * Class for normal text.  This class supports the following flags (only
     * one of which should be set):
     * {@link #TYPE_TEXT_FLAG_CAP_CHARACTERS},
     * {@link #TYPE_TEXT_FLAG_CAP_WORDS}, and.
     * {@link #TYPE_TEXT_FLAG_CAP_SENTENCES}.  It also supports the
     * following variations:
     * {@link #TYPE_TEXT_VARIATION_NORMAL}, and
     * {@link #TYPE_TEXT_VARIATION_URI}.  If you do not recognize the
     * variation, normal should be assumed.
     */
    public static final int TYPE_CLASS_TEXT = 0x00000001;

    /**
     * Flag for {@link #TYPE_CLASS_TEXT}: capitalize all characters.  Overrides
     * {@link #TYPE_TEXT_FLAG_CAP_WORDS} and
     * {@link #TYPE_TEXT_FLAG_CAP_SENTENCES}.  This value is explicitly defined
     * to be the same as {@link TextUtils#CAP_MODE_CHARACTERS}. Of course,
     * this only affects languages where there are upper-case and lower-case letters.
     */
    public static final int TYPE_TEXT_FLAG_CAP_CHARACTERS = 0x00001000;

    /**
     * Flag for {@link #TYPE_CLASS_TEXT}: capitalize the first character of
     * every word.  Overrides {@link #TYPE_TEXT_FLAG_CAP_SENTENCES}.  This
     * value is explicitly defined
     * to be the same as {@link TextUtils#CAP_MODE_WORDS}. Of course,
     * this only affects languages where there are upper-case and lower-case letters.
     */
    public static final int TYPE_TEXT_FLAG_CAP_WORDS = 0x00002000;

    /**
     * Flag for {@link #TYPE_CLASS_TEXT}: capitalize the first character of
     * each sentence.  This value is explicitly defined
     * to be the same as {@link TextUtils#CAP_MODE_SENTENCES}. For example
     * in English it means to capitalize after a period and a space (note that other
     * languages may have different characters for period, or not use spaces,
     * or use different grammatical rules). Of course,
     * this only affects languages where there are upper-case and lower-case letters.
     */
    public static final int TYPE_TEXT_FLAG_CAP_SENTENCES = 0x00004000;

    /**
     * Flag for {@link #TYPE_CLASS_TEXT}: the user is entering free-form
     * text that should have auto-correction applied to it. Without this flag,
     * the IME will not try to correct typos. You should always set this flag
     * unless you really expect users to type non-words in this field, for
     * example to choose a name for a character in a game.
     * Contrast this with {@link #TYPE_TEXT_FLAG_AUTO_COMPLETE} and
     * {@link #TYPE_TEXT_FLAG_NO_SUGGESTIONS}:
     * {@code TYPE_TEXT_FLAG_AUTO_CORRECT} means that the IME will try to
     * auto-correct typos as the user is typing, but does not define whether
     * the IME offers an interface to show suggestions.
     */
    public static final int TYPE_TEXT_FLAG_AUTO_CORRECT = 0x00008000;

    /**
     * Flag for {@link #TYPE_CLASS_TEXT}: the text editor (which means
     * the application) is performing auto-completion of the text being entered
     * based on its own semantics, which it will present to the user as they type.
     * This generally means that the input method should not be showing
     * candidates itself, but can expect the editor to supply its own
     * completions/candidates from
     * {@link android.view.inputmethod.InputMethodSession#displayCompletions
     * InputMethodSession.displayCompletions()} as a result of the editor calling
     * {@link android.view.inputmethod.InputMethodManager#displayCompletions
     * InputMethodManager.displayCompletions()}.
     * Note the contrast with {@link #TYPE_TEXT_FLAG_AUTO_CORRECT} and
     * {@link #TYPE_TEXT_FLAG_NO_SUGGESTIONS}:
     * {@code TYPE_TEXT_FLAG_AUTO_COMPLETE} means the editor should show an
     * interface for displaying suggestions, but instead of supplying its own
     * it will rely on the Editor to pass completions/corrections.
     */
    public static final int TYPE_TEXT_FLAG_AUTO_COMPLETE = 0x00010000;

    /**
     * Flag for {@link #TYPE_CLASS_TEXT}: multiple lines of text can be
     * entered into the field.  If this flag is not set, the text field
     * will be constrained to a single line. The IME may also choose not to
     * display an enter key when this flag is not set, as there should be no
     * need to create new lines.
     */
    public static final int TYPE_TEXT_FLAG_MULTI_LINE = 0x00020000;

    /**
     * Flag for {@link #TYPE_CLASS_TEXT}: the regular text view associated
     * with this should not be multi-line, but when a fullscreen input method
     * is providing text it should use multiple lines if it can.
     */
    public static final int TYPE_TEXT_FLAG_IME_MULTI_LINE = 0x00040000;

    /**
     * Flag for {@link #TYPE_CLASS_TEXT}: the input method does not need to
     * display any dictionary-based candidates. This is useful for text views that
     * do not contain words from the language and do not benefit from any
     * dictionary-based completions or corrections. It overrides the
     * {@link #TYPE_TEXT_FLAG_AUTO_CORRECT} value when set.
     * Please avoid using this unless you are certain this is what you want.
     * Many input methods need suggestions to work well, for example the ones
     * based on gesture typing. Consider clearing
     * {@link #TYPE_TEXT_FLAG_AUTO_CORRECT} instead if you just do not
     * want the IME to correct typos.
     * Note the contrast with {@link #TYPE_TEXT_FLAG_AUTO_CORRECT} and
     * {@link #TYPE_TEXT_FLAG_AUTO_COMPLETE}:
     * {@code TYPE_TEXT_FLAG_NO_SUGGESTIONS} means the IME should never
     * show an interface to display suggestions. Most IMEs will also take this to
     * mean they should not try to auto-correct what the user is typing.
     */
    public static final int TYPE_TEXT_FLAG_NO_SUGGESTIONS = 0x00080000;

    // ----------------------------------------------------------------------

    /**
     * Default variation of {@link #TYPE_CLASS_TEXT}: plain old normal text.
     */
    public static final int TYPE_TEXT_VARIATION_NORMAL = 0x00000000;

    /**
     * Variation of {@link #TYPE_CLASS_TEXT}: entering a URI.
     */
    public static final int TYPE_TEXT_VARIATION_URI = 0x00000010;

    /**
     * Variation of {@link #TYPE_CLASS_TEXT}: entering an e-mail address.
     */
    public static final int TYPE_TEXT_VARIATION_EMAIL_ADDRESS = 0x00000020;

    /**
     * Variation of {@link #TYPE_CLASS_TEXT}: entering the subject line of
     * an e-mail.
     */
    public static final int TYPE_TEXT_VARIATION_EMAIL_SUBJECT = 0x00000030;

    /**
     * Variation of {@link #TYPE_CLASS_TEXT}: entering a short, possibly informal
     * message such as an instant message or a text message.
     */
    public static final int TYPE_TEXT_VARIATION_SHORT_MESSAGE = 0x00000040;

    /**
     * Variation of {@link #TYPE_CLASS_TEXT}: entering the content of a long, possibly
     * formal message such as the body of an e-mail.
     */
    public static final int TYPE_TEXT_VARIATION_LONG_MESSAGE = 0x00000050;

    /**
     * Variation of {@link #TYPE_CLASS_TEXT}: entering the name of a person.
     */
    public static final int TYPE_TEXT_VARIATION_PERSON_NAME = 0x00000060;

    /**
     * Variation of {@link #TYPE_CLASS_TEXT}: entering a postal mailing address.
     */
    public static final int TYPE_TEXT_VARIATION_POSTAL_ADDRESS = 0x00000070;

    /**
     * Variation of {@link #TYPE_CLASS_TEXT}: entering a password.
     */
    public static final int TYPE_TEXT_VARIATION_PASSWORD = 0x00000080;

    /**
     * Variation of {@link #TYPE_CLASS_TEXT}: entering a password, which should
     * be visible to the user.
     */
    public static final int TYPE_TEXT_VARIATION_VISIBLE_PASSWORD = 0x00000090;

    /**
     * Variation of {@link #TYPE_CLASS_TEXT}: entering text inside of a web form.
     */
    public static final int TYPE_TEXT_VARIATION_WEB_EDIT_TEXT = 0x000000a0;

    /**
     * Variation of {@link #TYPE_CLASS_TEXT}: entering text to filter contents
     * of a list etc.
     */
    public static final int TYPE_TEXT_VARIATION_FILTER = 0x000000b0;

    /**
     * Variation of {@link #TYPE_CLASS_TEXT}: entering text for phonetic
     * pronunciation, such as a phonetic name field in contacts. This is mostly
     * useful for languages where one spelling may have several phonetic
     * readings, like Japanese.
     */
    public static final int TYPE_TEXT_VARIATION_PHONETIC = 0x000000c0;

    /**
     * Variation of {@link #TYPE_CLASS_TEXT}: entering e-mail address inside
     * of a web form.  This was added in
     * {@link android.os.Build.VERSION_CODES#HONEYCOMB}.  An IME must target
     * this API version or later to see this input type; if it doesn't, a request
     * for this type will be seen as {@link #TYPE_TEXT_VARIATION_EMAIL_ADDRESS}
     * when passed through {@link android.view.inputmethod.EditorInfo#makeCompatible(int)
     * EditorInfo.makeCompatible(int)}.
     */
    public static final int TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS = 0x000000d0;

    /**
     * Variation of {@link #TYPE_CLASS_TEXT}: entering password inside
     * of a web form.  This was added in
     * {@link android.os.Build.VERSION_CODES#HONEYCOMB}.  An IME must target
     * this API version or later to see this input type; if it doesn't, a request
     * for this type will be seen as {@link #TYPE_TEXT_VARIATION_PASSWORD}
     * when passed through {@link android.view.inputmethod.EditorInfo#makeCompatible(int)
     * EditorInfo.makeCompatible(int)}.
     */
    public static final int TYPE_TEXT_VARIATION_WEB_PASSWORD = 0x000000e0;

    // ----------------------------------------------------------------------
    // ----------------------------------------------------------------------
    // ----------------------------------------------------------------------

    /**
     * Class for numeric text.  This class supports the following flags:
     * {@link #TYPE_NUMBER_FLAG_SIGNED} and
     * {@link #TYPE_NUMBER_FLAG_DECIMAL}.  It also supports the following
     * variations: {@link #TYPE_NUMBER_VARIATION_NORMAL} and
     * {@link #TYPE_NUMBER_VARIATION_PASSWORD}.
     * <p>IME authors: If you do not recognize
     * the variation, normal should be assumed.</p>
     */
    public static final int TYPE_CLASS_NUMBER = 0x00000002;

    /**
     * Flag of {@link #TYPE_CLASS_NUMBER}: the number is signed, allowing
     * a positive or negative sign at the start.
     */
    public static final int TYPE_NUMBER_FLAG_SIGNED = 0x00001000;

    /**
     * Flag of {@link #TYPE_CLASS_NUMBER}: the number is decimal, allowing
     * a decimal point to provide fractional values.
     */
    public static final int TYPE_NUMBER_FLAG_DECIMAL = 0x00002000;

    // ----------------------------------------------------------------------

    /**
     * Default variation of {@link #TYPE_CLASS_NUMBER}: plain normal
     * numeric text.  This was added in
     * {@link android.os.Build.VERSION_CODES#HONEYCOMB}.  An IME must target
     * this API version or later to see this input type; if it doesn't, a request
     * for this type will be dropped when passed through
     * {@link android.view.inputmethod.EditorInfo#makeCompatible(int)
     * EditorInfo.makeCompatible(int)}.
     */
    public static final int TYPE_NUMBER_VARIATION_NORMAL = 0x00000000;

    /**
     * Variation of {@link #TYPE_CLASS_NUMBER}: entering a numeric password.
     * This was added in {@link android.os.Build.VERSION_CODES#HONEYCOMB}.  An
     * IME must target this API version or later to see this input type; if it
     * doesn't, a request for this type will be dropped when passed
     * through {@link android.view.inputmethod.EditorInfo#makeCompatible(int)
     * EditorInfo.makeCompatible(int)}.
     */
    public static final int TYPE_NUMBER_VARIATION_PASSWORD = 0x00000010;

    // ----------------------------------------------------------------------
    // ----------------------------------------------------------------------
    // ----------------------------------------------------------------------

    /**
     * Class for a phone number.  This class currently supports no variations
     * or flags.
     */
    public static final int TYPE_CLASS_PHONE = 0x00000003;

    // ----------------------------------------------------------------------
    // ----------------------------------------------------------------------
    // ----------------------------------------------------------------------

    /**
     * Class for dates and times.  It supports the
     * following variations:
     * {@link #TYPE_DATETIME_VARIATION_NORMAL}
     * {@link #TYPE_DATETIME_VARIATION_DATE}, and
     * {@link #TYPE_DATETIME_VARIATION_TIME}.
     */
    public static final int TYPE_CLASS_DATETIME = 0x00000004;

    /**
     * Default variation of {@link #TYPE_CLASS_DATETIME}: allows entering
     * both a date and time.
     */
    public static final int TYPE_DATETIME_VARIATION_NORMAL = 0x00000000;

    /**
     * Default variation of {@link #TYPE_CLASS_DATETIME}: allows entering
     * only a date.
     */
    public static final int TYPE_DATETIME_VARIATION_DATE = 0x00000010;

    /**
     * Default variation of {@link #TYPE_CLASS_DATETIME}: allows entering
     * only a time.
     */
    public static final int TYPE_DATETIME_VARIATION_TIME = 0x00000020;
}

參考文獻

Google Source: android.text.InputType

Google Source: R.styleable.TextView_inputType

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容