“九层之台,起于累土”——无论看上去多么美观的UI界面,开始都是先创建容器(ViewGroup的实例),然后不断地向容器中添加界面组件,最后形成一个美观的UI 界面的。
TextView直接继承了View,它还是EditText、Button两个UI组件类的父类。TextView的作用就是在界面上显示文本。
从功能上来看,TextView其实就是一个文本编辑器,只是Android关闭了它的文字编辑功能。如果开发者想要定义一个可编辑内容的文本框,则可以使用它的子类: EditText,EditText 允许用户编辑文本框中的内容。
TextView还派生了一个 CheckedTextView,CheckedTextView增加了一个 checked 状态,开发者可通过setChecked(boolean)和 isChecked()方法来改变、访问该组件的checked 状态。除此之外,该组件还可通过setCheckMarkDrawable()方法来设置它的勾选图标。
TextView及其子类的类图:

TextView和 EditText具有很多相似之处,它们之间的最大区别在于TextView不允许用户编辑文本内容,而 EditText则允许用户编辑文本内容。
TextView支持的XML属性及相关方法




实例——功能丰富的文本框
创建新项目

布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableEnd="@mipmap/ic_launcher"
android:text="我爱Android"
android:textSize="20pt" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:singleLine="true"
android:text="我爱Java我爱Java我爱Java我爱Java我爱Java我爱Java"
android:textAllCaps="true"
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="email|phone"
android:singleLine="true"
android:text="邮件是111111111@163.com,电话是18700000000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shadowColor="#00f"
android:shadowDx="10.0"
android:shadowDy="8.0"
android:shadowRadius="3.0"
android:text="测试文字"
android:textColor="#f00"
android:textSize="18pt" />
<CheckedTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="@drawable/ok"
android:text="可勾选的文本" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_border"
android:text="带边框的文本"
android:textSize="24pt" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_border2"
android:text="圆角边框、渐变背景的文本"
android:textSize="24pt" />
LinearLayout>
bg_border.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#0000"/>
<stroke android:width="2dp" android:color="#f00" />
shape>
bg_border2.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:topLeftRadius="20dp"
android:topRightRadius="10dp"
android:bottomRightRadius="20dp"
android:bottomLeftRadius="10dp"/>
<stroke android:width="4px" android:color="#f0f" />
<gradient android:startColor="#f00"
android:centerColor="#0f0"
android:endColor="#00f"
android:type="sweep"/>
shape>
运行效果

解释
这些属性同样适用于EditText和Button。
EditText 与 TextView的最大区别在于:EditText可以接受用户输入。
EditText组件最重要的属性是 inputType,该属性相当于HTML 的元素的type属性,用于将EditText设置为指定类型的输入组件。inputType能接受的属性值非常丰富,而且随着Android版本的升级,该属性能接受的类型还会增加。
EditText 还派生了如下两个子类:
布局
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<TableRow android:paddingStart="20dp"
android:paddingEnd="20dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="16sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写登录账号"
android:selectAllOnFocus="true"/>
TableRow>
<TableRow android:paddingStart="20dp"
android:paddingEnd="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="16sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"/>
TableRow>
<TableRow android:paddingStart="20dp"
android:paddingEnd="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="年龄:"
android:textSize="16sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
TableRow>
<TableRow android:paddingStart="20dp"
android:paddingEnd="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="生日:"
android:textSize="16sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="date"/>
TableRow>
<TableRow android:paddingStart="20dp"
android:paddingEnd="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="电话号码:"
android:textSize="16sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写您的电话号码"
android:selectAllOnFocus="true"
android:inputType="phone"/>
TableRow>
<Button
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"/>
TableLayout>
直接运行
