首先烘贴,我基于Android Studio進(jìn)行Android開發(fā),理由是Google已經(jīng)停止Eclipse上的ADT更新了间校。
其次碌宴,在設(shè)計(jì)每個Activity的layout.xml時,我會先在design界面進(jìn)行大體的規(guī)劃籍滴。
那么酪夷,今天要探討的內(nèi)容是出現(xiàn)在Pallete中的常用的Text相關(guān)控件。
具體見下圖紅框:
在知道了我要討論的控件具體在哪里后异逐,我們就可以正式開始了捶索!
一、概述
所有我要進(jìn)行探討的Text相關(guān)控件如下:
- TextView
- Plain Text
- Password
- Password (Numeric)
- Phone
- Postal Address
- Multiline Text
- Time
- Date
- Number
- Number (Signed)
- Number (Decimal)
- AutoCompleteTextView
- MultiAutoCompletTextView
- CheckedTextView
- TextInputLayout
別看這么多灰瞻,其實(shí)大體上僅需分為6種:
TextView腥例、EditText、AutoCompleteTextView酝润、MultiAutoCompleteTextView燎竖、CheckedTextView與android.support.design.widget.TextInputXXX。
為何我要這么分呢要销?這與它們在layout.xml中的聲明有關(guān)构回。
二、在layout.xml中的聲明
1. TextView
其聲明如下:
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
該控件的聲明用<TextView />括起來,其中id纤掸、width脐供、height為必須項(xiàng),其它屬性還有很多借跪,不用時可不寫政己。
2. EditText
Plain Text、Password掏愁、Password (Numeric)歇由、E-mail、Phone果港、Postal Address沦泌、Multiline Text、Time辛掠、Date谢谦、Number、Number (Signed)公浪、Number (Decimal)均屬于EditText他宛,他們的聲明如下:
<EditText
android:id="@+id/editText_PlainText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="@string/autofillHints"
android:ems="10"
android:hint="@string/hint"
android:inputType="XXX" />
它們的聲明用<EditText />括起來,其中id欠气、width、height镜撩、ems预柒、inputType為必須項(xiàng)。
這里先說一下ems袁梗,它是一種長度單位宜鸯,貌似是源自CSS,其用于表示一個字符的標(biāo)準(zhǔn)長度遮怜,適用于動態(tài)布局淋袖。也就是說,如果說系統(tǒng)默認(rèn)字體大小為1 ems锯梁,那么該控件可包含10個字符的大小即碗,并且如果窗口變大,該控件也會自動變大陌凳。
接著說下inputType剥懒,Plain Text、Password合敦、Password (Numeric)初橘、E-mail、Phone、Postal Address保檐、Multiline Text耕蝉、Time、Date夜只、Number赔硫、Number (Signed)、Number (Decimal)的不同在聲明時只主要存在這里的不同盐肃,以下列出這些控件所對應(yīng)的inputType:
- Plain Text —— textPersonName
- Password —— textPassword
- Password (Numeric) —— numberPassword
- E-mail —— textEmailAddress
- Phone —— phone
- Postal Address —— textPostalAddress
- Multiline Text —— textMultiLine
- Time —— time
- Date —— date
- Number —— number
- Number (Signed) —— numberSigned
- Number (Decimal) —— numberDecimal
你可能會發(fā)現(xiàn)爪膊,我的聲明里還包含了autofillHints、hint兩個屬性砸王。我寫這兩個屬性的理由是推盛,不寫會產(chǎn)生警告。
此處再提一句谦铃,我寫的是“@string/XXX”耘成,意思是該處的字符串引用自string.xml,這是一個好習(xí)慣驹闰,便于后期做多語言環(huán)境切換瘪菌。
我只寫了這么多屬性,可選屬性還有很多嘹朗,不用時可不寫师妙。
3. AutoCompleteTextView
其聲明如下:
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="@string/autofillHints"
android:hint="@string/hint" />
該控件的聲明用<AutoCompleteTextView />括起來,其中id屹培、width默穴、height為必須項(xiàng),寫autofillHints褪秀、hint兩個屬性仍是為了消除警告蓄诽,其它屬性還有很多,不用時可不寫媒吗。
4. MultiAutoCompleteTextView
其聲明如下:
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint" />
該控件的聲明用<MultiAutoCompleteTextView />括起來仑氛,其中id、width闸英、height為必須項(xiàng)锯岖,寫hint屬性仍是為了消除警告,其它屬性還有很多自阱,不用時可不寫嚎莉。
5. CheckedTextView
其聲明如下:
<CheckedTextView
android:id="@+id/checkedTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
該控件的聲明用<CheckedTextView />括起來,其中id沛豌、width趋箩、height為必須項(xiàng)赃额,其它屬性還有很多,不用時可不寫叫确。
6. android.support.design.widget.TextInputXXX
android.support.design.widget.TextInputXXX是指:
TextInputLayout的全稱為android.support.design.widget.TextInputLayout跳芳,且該layout在用鼠標(biāo)拖拽自動創(chuàng)建時,會包含一個TextInputEditText控件竹勉,這個被包含的EditText控件全稱為android.support.design.widget.TextInputEditText飞盆。它們擁有相同的前綴,所以將其概括為android.support.design.widget.TextInputXXX次乓。
它們的聲明如下:
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint" />
</android.support.design.widget.TextInputLayout>
TextInputLayout的聲明用<android.support.design.widget.TextInputLayout ></android.support.design.widget.TextInputLayout>包含起來吓歇,TextInputEditText的聲明用<android.support.design.widget.TextInputEditText />括起來,其中id票腰、width城看、height為二者的必須項(xiàng),寫TextInputEditText的hint屬性仍是為了消除警告杏慰,其它屬性還有很多测柠,不用時可不寫。
三缘滥、UI可視區(qū)別與使用區(qū)別
為了便于觀察我又在Activity.java文件內(nèi)對相關(guān)控件的相關(guān)屬性進(jìn)行了更改轰胁,后文會附上代碼,這里朝扼,我們先著眼于其運(yùn)行后各控件UI一開始視覺上的區(qū)別赃阀。
我們可以輕松的發(fā)現(xiàn),上述那么多控件其實(shí)只有兩種樣式吟税,TextView與EditText凹耙。
我們不妨去看一下Android文檔,仔細(xì)看一下每一個控件的實(shí)現(xiàn)肠仪,令人震驚的結(jié)果是,事實(shí)上备典,EditText也是繼承自TextView的异旧,而(Multi)AutoCompleteTextView和Edit同級也是直接繼承TextView并實(shí)現(xiàn)了補(bǔ)全提示,CheckedTextView更是同理提佣,其繼承TextView并實(shí)現(xiàn)了類似于CheckBox的選擇功能吮蛹!看著最為特殊的android.support.design.widget.TextInputXX,其實(shí)也很易于理解拌屏,在推行Material Design后潮针,為了更加好的UI體驗(yàn)而多了這么個控件,其本質(zhì)上不過就是LinearLayout包裹著一個EditText罷了倚喂。
那么總結(jié)一下每篷,花里胡哨的一大堆控件瓣戚,其實(shí)我們都可以用最基本的TextView來實(shí)現(xiàn),不過為了使用方便焦读、減少代碼量增強(qiáng)可讀性子库,各種已經(jīng)實(shí)現(xiàn)好了的不同Text控件,我們何樂而不用呢矗晃?
說了這么多仑嗅,我們看一下各個EditText具體的不同吧,其主要體現(xiàn)在獲得聚焦后調(diào)出的鍵盤樣式的不同张症。TextView和其余直接繼承TextView的控件這里就不在截圖了仓技,它們大多直接用于固定文字展示而并不注重于文本的Edit。
四俗他、源碼
源碼請見我的GitHub或碼云
Github:
https://github.com/mossanddws/MyAndroidLaboratory/tree/master/app/src/main
碼云:
https://gitee.com/mossand/MyAndroidLaboratory/tree/master/app/src/main