Android精讲--界面编程3(TextView及其子类)

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>

转载于:https://www.cnblogs.com/vvning/p/9414625.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值