首先,我基于Android Studio进行Android开发,理由是Google已经停止Eclipse上的ADT更新了。
其次,在设计每个Activity的layout.xml时,我会先在design界面进行大体的规划。
那么,今天要探讨的内容是出现在Pallete中的常用的Text相关控件。
具体见下图红框:
在知道了我要讨论的控件具体在哪里后,我们就可以正式开始了!
一、概述
所有我要进行探讨的Text相关控件如下:
- TextView
- Plain Text
- Password
- Password (Numeric)
- Phone
- Postal Address
- Multiline Text
- Time
- Date
- Number
- Number (Signed)
- Number (Decimal)
- AutoCompleteTextView
- MultiAutoCompletTextView
- CheckedTextView
- TextInputLayout
别看这么多,其实大体上仅需分为6种:
TextView、EditText、AutoCompleteTextView、MultiAutoCompleteTextView、CheckedTextView与android.support.design.widget.TextInputXXX。
为何我要这么分呢?这与它们在layout.xml中的声明有关。
二、在layout.xml中的声明
1. TextView
其声明如下:
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
该控件的声明用<TextView />括起来,其中id、width、height为必须项,其它属性还有很多,不用时可不写。
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为必须项。
这里先说一下ems,它是一种长度单位,貌似是源自CSS,其用于表示一个字符的标准长度,适用于动态布局。也就是说,如果说系统默认字体大小为1 ems,那么该控件可包含10个字符的大小,并且如果窗口变大,该控件也会自动变大。
接着说下inputType,Plain Text、Password、Password (Numeric)、E-mail、Phone、Postal Address、Multiline Text、Time、Date、Number、Number (Signed)、Number (Decimal)的不同在声明时只主要存在这里的不同,以下列出这些控件所对应的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
你可能会发现,我的声明里还包含了autofillHints、hint两个属性。我写这两个属性的理由是,不写会产生警告。
此处再提一句,我写的是“@string/XXX”,意思是该处的字符串引用自string.xml,这是一个好习惯,便于后期做多语言环境切换。
我只写了这么多属性,可选属性还有很多,不用时可不写。
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为必须项,写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为必须项,写hint属性仍是为了消除警告,其它属性还有很多,不用时可不写。
5. CheckedTextView
其声明如下:
<CheckedTextView
android:id="@+id/checkedTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
该控件的声明用<CheckedTextView />括起来,其中id、width、height为必须项,其它属性还有很多,不用时可不写。
6. android.support.design.widget.TextInputXXX
android.support.design.widget.TextInputXXX是指:
TextInputLayout的全称为android.support.design.widget.TextInputLayout,且该layout在用鼠标拖拽自动创建时,会包含一个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为二者的必须项,写TextInputEditText的hint属性仍是为了消除警告,其它属性还有很多,不用时可不写。
三、UI可视区别与使用区别
为了便于观察我又在Activity.java文件内对相关控件的相关属性进行了更改,后文会附上代码,这里,我们先着眼于其运行后各控件UI一开始视觉上的区别。
我们可以轻松的发现,上述那么多控件其实只有两种样式,TextView与EditText。
我们不妨去看一下Android文档,仔细看一下每一个控件的实现,令人震惊的结果是,事实上,EditText也是继承自TextView的,而(Multi)AutoCompleteTextView和Edit同级也是直接继承TextView并实现了补全提示,CheckedTextView更是同理,其继承TextView并实现了类似于CheckBox的选择功能!看着最为特殊的android.support.design.widget.TextInputXX,其实也很易于理解,在推行Material Design后,为了更加好的UI体验而多了这么个控件,其本质上不过就是LinearLayout包裹着一个EditText罢了。
那么总结一下,花里胡哨的一大堆控件,其实我们都可以用最基本的TextView来实现,不过为了使用方便、减少代码量增强可读性,各种已经实现好了的不同Text控件,我们何乐而不用呢?
说了这么多,我们看一下各个EditText具体的不同吧,其主要体现在获得聚焦后调出的键盘样式的不同。TextView和其余直接继承TextView的控件这里就不在截图了,它们大多直接用于固定文字展示而并不注重于文本的Edit。
Plain Text.png Password.png Password (Numeric).png E-mail.png Phone.png Address.png MultilineText.png Time.png Date.png Number.png Number (Signed).png Number (Decimal).png四、源码
源码请见我的GitHub或码云
Github:
https://github.com/mossanddws/MyAndroidLaboratory/tree/master/app/src/main
码云:
https://gitee.com/mossand/MyAndroidLaboratory/tree/master/app/src/main
网友评论