在这个 android 倒数计时器示例中,我们将实现一个计时器对象来显示进度进度条。我们将在本教程中构建的应用程序是测验应用程序中的一个有用组件,其中以图形方式显示完成该级别的剩余时间,以增强用户体验。
Android CountDownTimer
类用于安排倒计时,直到用户定义的未来某个时间,并在此过程中定期通知时间间隔。这个类是一个抽象类需要重写其方法才能在我们的项目中实现它。需要在我们的活动中添加以下行来导入该类:import android.os.CountDownTimer;
下面给出CountDownTimer类的相关方法。
-
synchronized final void cancel()
:用于取消倒计时
-
abstract void onFinish()
:定时器结束时触发此回调方法
-
abstract void onTick(long millisUntilFinished)
:此回调方法定期触发
-
synchronized final CountDownTimer start()
:该方法用于开始倒计时
下面给出了 CountDownTimer 类的公共构造函数的签名。CountDownTimer(long millisInFuture, long countDownInterval)
构造函数的参数定义如下:
-
未来的毫秒数:从调用start()到倒计时完成并调用onFinish()之间的未来毫秒数
-
倒计时间隔:沿途接收onTick(long)回调的时间间隔
在此项目中,我们将在重复调用 onTick() 方法时更新 ProgressBar 中的时间值。
Activity_main.xml 由两个按钮组成,即开始和停止计时器按钮以及用于显示时间的进度条。activity_main.xml
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="10"
android:minHeight="50dp"
android:minWidth="200dp"
android:progress="0"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Timer"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="61dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Timer"
android:id="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:layout_below="@+id/progressBar" />
</RelativeLayout>
MainActivity.java 如下:
package com.journaldev.countdowntimer;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
Button start_timer,stop_timer;
MyCountDownTimer myCountDownTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar=(ProgressBar)findViewById(R.id.progressBar);
start_timer=(Button)findViewById(R.id.button);
stop_timer=(Button)findViewById(R.id.button2);
start_timer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myCountDownTimer = new MyCountDownTimer(10000, 1000);
myCountDownTimer.start();
}
});
stop_timer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myCountDownTimer.cancel();
}
});
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
int progress = (int) (millisUntilFinished/1000);
progressBar.setProgress(progressBar.getMax()-progress);
}
@Override
public void onFinish() {
finish();
}
}
}
In the above code we’ve defined an Anonymous Inner Class called MyCountDownTimer
. In this example we’ve set a Timer for 10 seconds that updates after every second. By default the timer displays/updates the time in decreasing order ( as its named CountDown!), Hence to show the progress in increasing order we’ve subtracted the time from the max time. The timer once stopped restarts from the beginning. Below is our android countdown timer app in action. This brings an end to countdown timer android tutorial. You can download the final Android CountDownTimer Project from the below link.
下载带有 ProgressBar 的 Android CountDownTimer 项目
参考:官方文档
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)