Android自带的倒计时CountDownTimer
来源:互联网 发布:win10不能下载软件 编辑:程序博客网 时间:2024/06/08 12:26
Android自带的倒计时CountDownTimer
CountDownTimer类介绍:
CountDownTimer类比较简单,总共就一个构造和4个方法。内部是通过handler实现。
CountDownTimer(long time,long interval):参数time是总时间,interval是间隔时间。
start():开始倒计时的方法。
cancel():取消倒计时的方法。
onTink(long time):抽象方法,每个间隔时间一到就会调用一次,需要自己实现。参数time是指剩下的时间。
onFinish():抽象方法,倒计时完成的方法。
CountDownTimer示例:
public class MainActivity extends AppCompatActivity { private TextView textView; private MyCountDownTimer timer; private final long TIME = 60 * 1000L; private final long INTERVAL = 1000L; public class MyCountDownTimer extends CountDownTimer { public MyCountDownTimer(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onTick(long millisUntilFinished) { long time = millisUntilFinished / 1000; if (time <= 59) { textView.setText(String.format("倒计时开始 00:%02d", time)); } else { textView.setText(String.format("倒计时开始 %02d:%02d", time / 60, time % 60)); } } @Override public void onFinish() { textView.setText("倒计时结束 00:00"); cancelTimer(); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); textView = (TextView) findViewById(R.id.tv); startTimer(); } public void start(View view) { startTimer(); } public void cancel(View view) { textView.setText("倒计时结束 00:00"); cancelTimer(); } /** * 开始倒计时 */ private void startTimer() { if (timer == null) { timer = new MyCountDownTimer(TIME, INTERVAL); } timer.start(); } /** * 取消倒计时 */ private void cancelTimer() { if (timer != null) { timer.cancel(); timer = null; } } @Override protected void onDestroy() { super.onDestroy(); cancelTimer(); }}
布局:
<?xml version="1.0" encoding="utf-8"?><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:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="16dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="start" android:text="开始" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="cancel" android:text="结束" /></LinearLayout>
截图:
0 0
- Android自带的倒计时CountDownTimer
- Android自带的一个用于倒计时的类CountDownTimer
- Android倒计时CountDownTimer的使用
- Android倒计时CountDownTimer的使用
- android 倒计时CountDownTimer的使用
- Android倒计时 CountDownTimer的使用
- Chronometer android自带的倒计时控件
- Android自带计时器类CountDownTimer
- Android自带计时器类CountDownTimer
- CountDownTimer---android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- Android倒计时功能的实现(CountDownTimer)
- Android 倒计时功能的实现(CountDownTimer)
- 数组的循环移动
- 使用DrawerLayout实现侧拉菜单
- 关于plsql的commit,事务回滚 rollback
- VC++ 通过ADO连接数据库查询时返回空值报错的解决方案
- 二叉树中和为某一值的路径
- Android自带的倒计时CountDownTimer
- HTML 提高页面加载速度的方法
- TP3.2的URL重写省略index.php问题
- 机器学习实战---读书笔记: 第3章 决策树
- [Weex-BBQ]Weex项目中引用css样式的三种姿势
- Oracle中查看所有表和字段以及表注释.字段注释
- s标签大全
- Android5.0之NavigationView的使用
- 模板的分离编译