在这个Android倒计时计时器示例中,我们将实现一个Timer对象来在ProgressBar.中显示进度我们将在本教程中构建的应用程序是测验应用程序中的一个有用组件,在该应用程序中,完成考试的剩余时间将以图形形式显示,以增强用户体验。
AndroidDowDonTime
AndroidCountDownTimer
类用于计划倒计时,直到用户定义的未来时间,并在此过程中每隔一段时间定期通知。这个类是一个抽象的class,需要重写它的方法才能在我们的项目中实现它。需要在我们的活动中添加以下行来导入类:import android.os.CountDownTimer;
CountDownTimer类的相关方法如下所示。
synchronized final void cancel()
:用于取消倒计时abstract void onFinish()
:当计时器结束时,将触发此回调方法abstract void onTick(long millisUntilFinished)
:此回调方法定期触发synchronized final CountDownTimer start()
:该方法用于启动倒计时
CountDownTimer类的公共构造函数的签名如下所示。CountDownTimer(long milisInFuture,long count DownInterval)
构造函数的参数定义如下:
- MillisInFuture :从调用Start()到完成倒计时并调用onFinish()的未来毫秒数
- CountDownInterval :接收onTick(Long)回调的间隔时间
在这个项目中,我们将在反复调用onTick()方法时更新ProgressBar中的时间值。
Android倒计时计时器示例项目结构
Android倒计时计时器代码
Active_main.xml由两个按钮组成,即Start和Stop Timer按钮和一个显示时间的ProgressBar。active_main.xml
1<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
2 xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent"
3 android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
4 android:paddingRight="@dimen/activity_horizontal_margin"
5 android:paddingTop="@dimen/activity_vertical_margin"
6 android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
7
8 <ProgressBar
9 android:id="@+id/progressBar"
10 style="?android:attr/progressBarStyleHorizontal"
11 android:layout_width="wrap_content"
12 android:layout_height="wrap_content"
13 android:indeterminate="false"
14 android:max="10"
15 android:minHeight="50dp"
16 android:minWidth="200dp"
17 android:progress="0"
18 android:layout_centerVertical="true"
19 android:layout_alignParentRight="true"
20 android:layout_alignParentEnd="true"
21 android:layout_alignParentLeft="true"
22 android:layout_alignParentStart="true" />
23
24 <Button
25 android:layout_width="wrap_content"
26 android:layout_height="wrap_content"
27 android:text="Start Timer"
28 android:id="@+id/button"
29 android:layout_alignParentTop="true"
30 android:layout_centerHorizontal="true"
31 android:layout_marginTop="61dp" />
32
33 <Button
34 android:layout_width="wrap_content"
35 android:layout_height="wrap_content"
36 android:text="Stop Timer"
37 android:id="@+id/button2"
38 android:layout_centerHorizontal="true"
39 android:layout_marginTop="46dp"
40 android:layout_below="@+id/progressBar" />
41
42</RelativeLayout>
MainActivity.java如下所示:
1package com.journaldev.countdowntimer;
2
3import android.os.CountDownTimer;
4import android.support.v7.app.AppCompatActivity;
5import android.os.Bundle;
6import android.view.Menu;
7import android.view.MenuItem;
8import android.view.View;
9import android.widget.Button;
10import android.widget.ProgressBar;
11import android.widget.TextView;
12
13public class MainActivity extends AppCompatActivity {
14
15 ProgressBar progressBar;
16 Button start_timer,stop_timer;
17 MyCountDownTimer myCountDownTimer;
18
19 @Override
20 protected void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.activity_main);
23
24 progressBar=(ProgressBar)findViewById(R.id.progressBar);
25 start_timer=(Button)findViewById(R.id.button);
26 stop_timer=(Button)findViewById(R.id.button2);
27
28 start_timer.setOnClickListener(new View.OnClickListener() {
29 @Override
30 public void onClick(View v) {
31
32 myCountDownTimer = new MyCountDownTimer(10000, 1000);
33 myCountDownTimer.start();
34
35 }
36 });
37
38 stop_timer.setOnClickListener(new View.OnClickListener() {
39 @Override
40 public void onClick(View v) {
41
42 myCountDownTimer.cancel();
43
44 }
45 });
46
47 }
48
49 public class MyCountDownTimer extends CountDownTimer {
50
51 public MyCountDownTimer(long millisInFuture, long countDownInterval) {
52 super(millisInFuture, countDownInterval);
53 }
54
55 @Override
56 public void onTick(long millisUntilFinished) {
57
58 int progress = (int) (millisUntilFinished/1000);
59
60 progressBar.setProgress(progressBar.getMax()-progress);
61 }
62
63 @Override
64 public void onFinish() {
65 finish();
66 }
67 }
68}
在上面的代码中,我们定义了一个名为MyCountDownTimer
的匿名内部类。在本例中,我们设置了一个10秒的计时器,该计时器每秒钟更新一次。默认情况下,计时器以递减顺序显示/更新时间(正如其命名的倒计时!),因此为了以递增顺序显示进度,我们从最大时间中减去了时间。计时器一旦停止,将从头重新启动。以下是我们的Android倒计时计时器应用程序的运行情况。这结束了倒计时计时器安卓教程。您可以通过下面的链接下载最终的Android CountDownTimer项目 。
下载Android CountDownTimer with ProgressBar项目
参考资料:官方Documentation