自定义倒计时TextView控件

本文介绍了如何创建一个自定义的TextView控件,用于实现倒计时功能。通过继承TextView并在资源布局中引用,可以轻松实现倒计时显示。
摘要由CSDN通过智能技术生成

一、首先要创建一个自定义的TextView

  1. 创建一个继承TextView的控件
  2. 资源布局中引用该控件
创建自定义的TextView控件
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class TimerTextView extends TextView {

	public TimerTextView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public TimerTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public TimerTextView(Context context) {
		super(context);
	}
}

在布局中引用该控件
<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >

    <com.example.timertextviewdemo.TimerTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

二、完善这个TimerTextView加入倒计时功能。

要另启用一个线程,来实现倒计时的功能。首先要实现Runnable接口。为了控制这个倒计时,需要有个isRun变量,查看程序是否正在运行。提供一个start和stop方法来控制计时器。
第二部主要实现倒计时功能。需要一个setTimes和countdown方法。如下:
imp
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值