下面列出了TextView及其子类的类图关系:
TextView与EditText具有很多相似的地方,它们最大的不同在于:TextView不允许用户编辑文本内容,EditText允许用户编辑文本内容。
TextView提供大量XML属性,这些XML属性适用于TextView以及它的子类,下面列出这些XML属性以及相关方法:
1、EditText:
EditText组件最重要的属性是inputType,该属性相当于HTML的<input .../>元素的type属性。
EditText派生如下两个子类:
(1)AutoCompleteTextView:带有自动完成功能的EditText,该类常与Adapter结合使用
(2)ExtractEditText:并不是UI组件,而使EditText组件的底层服务类,负责提供全屏输入法支持
2、Button:
Button继承自TextView,主要在UI界面上生成一个按钮,当用户单击时,触发一个onClick事件,
可以使用android:background属性为按钮增加背景颜色或者背景图片,如果要实现动态图片,则使用Drawable对象来实现。
下面只是一个简单的例子:
activity_button.xml如下:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ButtonActivity" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shadowColor="#aa5"
android:shadowDx="5"
android:shadowDy="5"
android:shadowRadius="1"
android:text="@string/bn1"
android:textSize="12pt" />
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/red"
android:text="@string/bn2"
android:textSize="10pt" >
</Button>
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_selector"
android:text="@string/bn3"
android:textSize="11pt" >
</Button>
</TableRow>
</TableLayout>
button_selector.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/blue" android:state_pressed="true"></item>
<item android:drawable="@drawable/purple" android:state_pressed="false"></item>
</selector>
这里的button3就是实现的按钮动态图片,要好好体会。
(1)RadioButton:单选按钮,每次只能选中一个,常与RadioGroup一起使用
(2)CheckBox:复选框,每次可以选中若干个
(3)ToggleButton:状态开关按钮
(4)Switch:开关
ToggleButton、Switch与CheckBox很相似,但是它们之间的主要区别在功能上,ToggleButton、Switch通常用于切换程序中的某种状态,
ToggleButton支持的XML属性:
Switch支持的XML属性:
3、AnalogClock/DigitalClock :
DigitalClock本身就继承了TextView,只是里面显示的内容是当前时间,DigitalClock继承了TextView的所有属性。
AnalogClock继承了View组件,重写View的OnDraw方法,它会在View绘制模拟时钟,AnalogClock的属性如下:
4、Chronometer:
计时器组件,该组件与DigitalClock一样,都继承TextView,但是Chronometer并不显示当前时间,而是显示从某个起始时间开始,一共过去多少时间。
Chronometer支持如下的方法:
下面是几种SystemClock的常用方法: