Android精讲--界面编程3

TextVuew


TextView其实就是一个文本编辑器,只是Android关闭了它的文字编辑功能。

TextView直接继承了View,与其子类的类图关系:

TextView及其子类的类图

# TextView常用的xml属性及相关方法

xml属性 相关方法 说明
android:autoLink setAutoLinkMask(int) 是否将符合指定格式的文本转换为可单击的超链接形式
android:autoText setKeyLinstener(KeyLinstener) 控制是否将的URL、email等地址自动转换为可单击的链接
android:linksClickable setLinksClickable(boolean) 控制该文本框的URL、email等链接是否具可以点击
android:capitalize setKeyLinstener(KeyLinstener) 控制是否将用户输入的文本转换为大写字母
android:cursorVisible setCursorVisiblek(boolean) 设置该文本框的光标是否可见
android:drawableBottom setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable) 在文本框底端处绘制图像
android:drawableTop setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable) 在文本框顶端处绘制图像
android:drawableEnd setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable) 在文本框结尾处绘制图像
android:drawableLeft setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable) 在文本框左边处绘制图像
android:drawableRight setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable) 在文本框右边处绘制图像
android:drawablePadding setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable) 设置文本框内文本与图形的间距
android:drawableStart setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable) 在文本框开始处绘制图像
android:editable 设置文本框是否允许被编辑
android:ellipsize setEllipsize(TextUitls,TruncateAt) 当显示文本超过了TextView的宽度时,如何处理文本
android:ems setEms(int) 设置文本框的宽度
android:height setHeight(int) 设置文本框的高度
android:fontFamily setTypeface(Typeface) 设置文本框内文本的字体
android:gravity setGravity(int) 设置文本框内文本的对齐方式
android:hint setHint(int) 设置文本框内容为空时,文本框内默认显示的提示文字
android:inputType setRawInputType(int) 设置文本框输入方式
android:lines setLines(int) 设置文本框默认占几行
android:maxEms setMaxEms(int) 设置文本框最大宽度
android:maxHeight setMaxHeight(int) 设置文本框最大高度
android:maxLenghth setMaxLenghth(int) 设置文本框最大字符长度
android:maxWidth setMaxWidth(int) 设置文本框最大宽度
android:maxLines setMaxLines(int) 设置文本框最多占几行
android:maxEms setMaxEms(int) 设置文本框最小宽度
android:minHeight setMinHeight(int) 设置文本框最小高度
android:minLenghth setMinxLenghth(int) 设置文本框最小长度
android:minWidth setMinWidth(int) 设置文本框最小宽度
android:minLines setMinLines(int) 设置文本框最少占几行
android:passwordd setTransformatinMethod(TransformatinMethod) 设置文本框是一个密码框
android:phoneNumber setKeyListener(KeyListener) 设置文本框只接受电话号码
android:scrollHorizontally setHorizontallyScrolling(boolean) 设置文本框不够显示全部内容时是否允许水平滚动
android:selectAllOnFocus setSelectAllOnFocus(boolean) 如果文本框内的内容可选择,设置是否当它获得焦点时自动选择所有文本
android:shawdowColor setshadowLayer(float,float,float,int) 设置文本框内文本的阴影的颜色
android:shawdowDx setshadowLayer(float,float,float,int) 设置文本框内文本的阴影在水平方向的偏移
android:shawdowDy setshadowLayer(float,float,float,int) 设置文本框内文本的阴影在垂直方向的偏移
android:shawdowRadius setshadowLayer(float,float,float,int) 设置文本框内文本的阴影的模糊程度,值越大,阴影越模糊
android:singleLine setTransformationMethod() 设置文本框是否为单行模式
android:text setText(CharSequence) 设置是否将文本框文本的内容
android:textAllCaps setAllCaps(boolean) 设置是否将文本框中所有字幕显示为大写字母
android:textAppearance 设置文本框的颜色、字体、大小等样式
android:textColor setTextColor(ColorStateList) 设置文本框中文本的颜色
android:textColorHighlight setHighlightColor(int) 设置文本框被选中时的颜色
android:textColorHint setHintTextColor(int) 设置文本框中提示文本的颜色
android:textColorLink setLinkTextColor(int) 设置文本框中链接的颜色
android:textIsSelectable setTextSelectedable() 设置该文本框不能编辑时,文本框内的文本是否可以被选中
android:textSize setTextSize(float) 设置文本框的字体大小
android:textStyle setTypeface(Typeface) 设置文本框内字体风格,如粗体、斜体等
android:width setWidth(int) 设置该文本框的宽度

示例:不同颜色、字体、带链接的文本

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <!-- 设置字体为20pt,文本框结尾处绘制图片  -->
    <TextView
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="我爱Java"
        android:textSize="20pt"
        android:drawableEnd="@drawable/ic_launcher"/>
    <!-- 设置中间省略, 所有字母大写 -->
    <TextView
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:singleLine="true" 
        android:text="我爱Java我爱Java我爱Java我爱Java我爱Java我aaaJava"
        android:ellipsize="middle"
        android:textAllCaps="true"/>
    <!-- 对邮件、电话增加链接 -->
    <TextView
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:singleLine="true" 
        android:text="邮件是kongyeeku@163.com,电话是02088888888"
        android:autoLink="email|phone"/>
    <!-- 设置文字颜色 、大小,并使用阴影 -->
    <TextView
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="测试文字"
        android:shadowColor="#0000ff"
        android:shadowDx="10.0"
        android:shadowDy="8.0"
        android:shadowRadius="3.0"
        android:textColor="#f00"
        android:textSize="18pt"/>
    <!-- 测试密码框 -->
    <TextView android:id="@+id/passwd"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        android:password="true"/>
    <!-- 测试CheckedTextView
    通过checkMark设置该文本框的勾选图标
     -->
    <CheckedTextView
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="可勾选的文本"
        android:checkMark="@drawable/ok"/>
</LinearLayout>

EditText


EditText与TextView的最大区别在于:EditText可以接受用户输入。
EditText组件最重要的属性是inputType,用于EditText为指定类型的输入组件。

EditText还派生了如下两个子类:

  • AutoCompleteTextView:带有自动完成功能的EditText,实际上该组件的命名不太恰当。应该叫AutoCompleteEditText更合适。该类通常需要与Adapter结合使用。
  • ExtractEditText:它并不是UI组件,而是EditText组件的底层服务类,负责提供全屏输入法支持。

示例:用户友好的输入界面

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1"
    >
<TableRow>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="用户名:"
    android:textSize="16sp"
    />  
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="请填写登录帐号"
    android:selectAllOnFocus="true"
    />
</TableRow>
<TableRow>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="密码:"
    android:textSize="16sp" 
    />
<!-- android:inputType="numberPassword"表明只能接收数字密码 -->
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="numberPassword"
    />
</TableRow>
<TableRow>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="年龄:"
    android:textSize="16sp" 
    />
<!-- inputType="number"表明是数值输入框 -->
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    />
</TableRow>
<TableRow>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="生日:"
    android:textSize="16sp" 
    />
<!-- inputType="date"表明是日期输入框 -->
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="date"
    />
</TableRow>
<TableRow>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="电话号码:"
    android:textSize="16sp" 
    />
<!-- inputType="phone"表明是输入电话号码的输入框 -->
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="请填写您的电话号码"
    android:selectAllOnFocus="true"
    android:inputType="phone"
    />
</TableRow>
<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="注册"
    />
</TableLayout>

按钮(Button)


Button继承了TextView , 它主要是在UI界面上生成一个按钮,该按钮可以供用户单击,当用户单击按钮时, 按钮会触发 一个onClick事件。关于onClick事件编程的简单示例,本书前面己经见到很多,后面还会详细介绍Android的事件编程。
按钮使用起来比较容易,可以通过为按钮指定android:background属性为按钮增加背景颜色或背景图片,如果将背景图片设为不规则的背景图片,则可以开发出各种不规则形状的按钮。
如果只是使用普通的背景颜色或背景图片,那么这些背景是固定的,不会随着用户的动作而改变。如果需要让按钮的背景 颜 色、背景图片随用户动作动态改变,则可以考虑使用自定义Drawable对象来实现。

示例:按钮、圆形按钮、带文字的图片按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <!-- 文字带阴影的按钮 -->   
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文字带阴影的按钮"
        android:textSize="12pt"
        android:shadowColor="#aa5"
        android:shadowRadius="1"
        android:shadowDx="5"
        android:shadowDy="5"
    />  
    <!-- 普通文字按钮 --> 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/red"
        android:text="普通按钮"
        android:textSize="10pt"
    />
    <!-- 带文字的图片按钮-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_selector"
        android:textSize="11px"
        android:text="带文字的图片按钮"
    />
</LinearLayout>

第三个按钮有点特殊,它指定了 android:background属性为@drawable/button_selector,该属性值引用一个Drawable资源,该资源对应的XML文件如:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 指定按钮按钮下时的图片 -->
    <item android:state_pressed="true"
        android:drawable="@drawable/red"
    />
    <!-- 指定按钮松开时的图片 --> 
    <item android:state_pressed="false"
        android:drawable="@drawable/purple"
    />
</selector>

使用9Patch图片作为按钮背景


9Patch图片是一种特殊的PNG图片,这种图片以 .9.png结尾,它在原始图片四周各添加一个宽度为1像素的线条,这4条线就决定了该图片的缩放规则、内容昆示规则。
左侧和上侧的直线共同决定了图片的缩放区域:以左边直线为左边界绘制矩形,它覆盖的区域可以在纵向缩放; 以上面直 线为上边界绘制矩形,它覆盖的区域可以水平缩 放; 它们二者的交集可以在两个方向上缩放。
右侧和下侧的直线共同决定图片的内容显示区域: 以右边直线为右边界绘制矩形,以下边直线为下边界绘制矩形,它们二者的交集就是图片的内容显示区域 。

图片缩放区域和内容显示区域

Android为制作9Patch图片提供了 draw9patch工具,该工具位于Android SDK安装路径的tools目录下,进入该目录双击draw9patch.bat文件,即可启动该工具。

单选按钮(RadioButton)与复选框(CheckBox)


RadioButton、CheckBox与普通按钮不同的是,它们多了一个可选中的功能,因此RadioButton、CheckBox都可额外指定一个android:checked属性,该属性用于指定RadioButton、CheckBox初始时是否被选中。
RadioButton与CheckBox的不同之处在于,一组RadioButton只能选中其中一个,因此RadioButton通常要与RadioGroup—起使用,用于定义一组单选按钮。

实例:利用单选按钮、爱选框获取用户信息

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TableRow>
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="性别:"
        android:textSize="16px"/>
    <!-- 定义一组单选框 -->
    <RadioGroup android:id="@+id/rg"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal">
        <!-- 定义两个单选框 -->
        <RadioButton android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/male"
            android:text="男"
            android:checked="true"/>
        <RadioButton android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/female"
            android:text="女"/>
    </RadioGroup>
    </TableRow> 
    <TableRow>
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="喜欢的颜色:"
        android:textSize="16px"
        />
    <!-- 定义一个垂直的线性布局 -->
    <LinearLayout 
        android:layout_gravity="center_horizontal"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <!-- 定义三个复选框 -->
        <CheckBox android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="红色"
            android:checked="true"/>
        <CheckBox android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="蓝色"/>
        <CheckBox android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绿色"/>
    </LinearLayout>
    </TableRow>
    <TextView 
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</TableLayout>

如果在XML布局文件中默认勾选了某个单选按钮,則必须为该组单选按钮的每个按钮指定android:id属性值,否則这组单选按钮不能正常工作.

为了监听单选按钮、复选框的勾选状态的改变,可以为它们添加事件监听器。例如下面Activity为RadioGroup添加了事件监听器,该监听器可以监听这组单选按钮的勾选状态的改变。

package org.crazyit.ui;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
 * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class CheckButtonTest extends Activity
{
    RadioGroup rg;
    TextView show;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 获取界面上rg、show两个组件
        rg = (RadioGroup) findViewById(R.id.rg);
        show = (TextView) findViewById(R.id.show);
        // 为RadioGroup组件的OnCheck事件绑定事件监听器
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                // 根据用户勾选的单选按钮来动态改变tip字符串的值
                String tip = checkedId == R.id.male ?
                        "您的性别是男人": "您的性别是女人";
                // 修改show组件中的文本。
                show.setText(tip);
            }
        });
    }
}

状态开关按钮(ToggleButton)与开关(Switch)


状态开关按钮(ToggleButton)与开关(Switch)也是由Button派生出来的,因此它们的本质也是按钮,Button支持的各种属性、方法也适用于ToggleButton和Switch。从功能上来看,ToggleButton、Switch与CheckBox复选框非常相似,它们都可以提供两个状态。不过ToggleButton、Switch与CheckBox的区别主要体现在功能上,ToggleButton、Switch通常用于切换程序中的某种状态。

# ToggleButton支持的XML属性及相关方法说明

XML属性 相关方法 说明
android:checked setChecked(boolean) 设置该按钮是否被选中
android:textOff 设置当该按钮的状态关闭时显示的文本
android:textOn 设置当该按钮的状态打开时显示的文本

# Switch支持的XML属性及相关方法说明

XML属性 相关方法 说明
android:checked setChecked(boolean) 设置该开关是否被选中
android:switchMinWidth setSwitchMinWidth(int) 设置该开关的最小宽度
android"switchPadding setSwitchPadding(int) 设置开关与标题文本之间的空白
android:switchTextAppearance setSwitchTextAppearance(Context,int) 设置该开关图标上的文本样式
android:textOff setTextOff(CharSequence) 设置该开关的状态关闭时显示的文本
android:textOn setTextIOn(CharSequence) 设置该开关的状态打开时显示的文本
android:textStyle setSwitchTypeface(Typeface) 设置该开关的文本的风格
android:thumb setThumbResource(int) 指定使用自定义Drawable绘制该开关的开关按钮
android:track setThumbResource(int) 指定使用自定义Drawable绘制该开关的开关轨道
android:typeface setSwitchTypeface(Typeface) 设置该开关的文本的字体风格

实例:动态控制布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <!-- 定义一个ToggleButton按钮 -->
    <ToggleButton android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="横向排列"
        android:textOn="纵向排列"
        android:checked="true"/>
    <Switch android:id="@+id/switcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="横向排列"
        android:textOn="纵向排列"
        android:thumb="@drawable/check"
        android:checked="true"/>
    <!-- 定义一个可以动态改变方向的线性布局 -->
    <LinearLayout android:id="@+id/test"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试按钮一"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试按钮二"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试按钮三"/>
    </LinearLayout>
</LinearLayout>

接下来我们为ToggleButton按钮、Switch按钮绑定监听器,当它的选中状态发生改变时,程序通过代码来改变LinearLayout的布局方向。

package org.crazyit.ui;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.ToggleButton;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
 * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class ToggleButtonTest extends Activity
{
    ToggleButton toggle;
    Switch switcher;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        toggle = (ToggleButton)findViewById(R.id.toggle);
        switcher = (Switch)findViewById(R.id.switcher);
        final LinearLayout test = (LinearLayout)findViewById(R.id.test);
        OnCheckedChangeListener listener = new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton button
                    , boolean isChecked)
            {
                if(isChecked)
                {
                    //设置LinearLayout垂直布局
                    test.setOrientation(1);
                }
                else
                {
                    //设置LinearLayout水平布局
                    test.setOrientation(0);
                }
            }
        };
        toggle.setOnCheckedChangeListener(listener);
        switcher.setOnCheckedChangeListener(listener);
    }
}

时钟(AnalogClock)与DigitalClock


时钟UI组件是两个非常简单的组件,DigitalClock本身就继承了TextView——也就是说它本身就是文本框,只是它里面显示的内容总是当前时间。与TextView不同的是,为DigitalClock设置android:text属性没什么作用。
AnalogClock则继承了View组件,它重写了View的OnDraw方法,它会在View上绘制模拟时钟。

# AnalogClock支持的XML属性的说明

XML属性 说明
android:dial 设置该模拟时钟的表盘使用的图片
android:hand_hour 设置该模拟时钟的时针表盘使用的图片
android:hand_minute 设置该模拟时钟的分针使用的图片

DigitalClock和AnalogClock都会显示当前时间,不同的是,DigitalClock显示数字时钟,可以显示当前的秒数:AnalogClock显示模拟时钟,不会显示当前秒数。

实例:手机里的“劳力士”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal">
    <!-- 定义模拟时钟 -->
    <AnalogClock  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>
    <!-- 定义数字时钟 -->
    <DigitalClock
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="14pt"
        android:textColor="#f0f"
        android:drawableRight="@drawable/ic_launcher"/>
    <!-- 定义模拟时钟,并使用自定义表盘、时针图片 -->
    <AnalogClock  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:dial="@drawable/watch"
        android:hand_minute="@drawable/hand"/>
</LinearLayout>

计时器(Chornometer)


Android还提供了一个计时器组件:Chronometer,该组件与DigitalClock都继承自TextView,因此它们都会显示一段文本。但Chronometer并不显示当前时间,它显示的是从某个起始
时间开始,一共过去了多长时间。
Chronometer的用法也很简单,它只提供了一个android:format属性,用于指定计时器的计时格式。除此之外,Chronometer支持如卞常用方法。

  • setBase(longbase): 设置计时器的起始时间。
  • setFormat(Stringformat):设置M示时间的格式。
  • start():开始计时,
  • stop():停止计时。
  • setOnChronometerTickListener(Chronometer.OnChronometerTickListenerlistener):为计时器绑定事件监听器,当计时器改变时触发该监听器。

实例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal">
    <Chronometer
        android:id="@+id/test"  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="12pt"
        android:textColor="#ffff0000"/>
    <Button
        android:id="@+id/start"  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="启动"/>      
</LinearLayout>
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,902评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,037评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,978评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,867评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,763评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,104评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,565评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,236评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,379评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,313评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,363评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,034评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,637评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,719评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,952评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,371评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,948评论 2 341

推荐阅读更多精彩内容