Android 中不用線程如何實(shí)現(xiàn)倒計(jì)時(shí)
需求:
有多個(gè)組件可以開啟倒計(jì)時(shí),正常情況下默認(rèn)倒計(jì)時(shí)時(shí)間終了后更新UI,另,用戶可以取消指定倒計(jì)時(shí)。
這里使用CountDownTimer進(jìn)行倒計(jì)時(shí),其中回調(diào)函數(shù)onFinish是在倒計(jì)時(shí)終了時(shí)回調(diào),onTick是在倒計(jì)時(shí)開始時(shí)回調(diào),用戶可以使用CountDownTimer對(duì)象的cancel方法取消倒計(jì)時(shí)。
這樣做的好處:不需要使用繁瑣的線程去控制倒計(jì)時(shí),更方便的進(jìn)行UI更新。
上代碼:
MainActivity
package test.demo.countdowntest;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button bt1, bt2, bt3;
private ProgressBar pb1, pb2, pb3;
private MyCount mc1,mc2, mc3;
private boolean mc1Click = false;
private boolean mc2Click = false;
private boolean mc3Click = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = ((Button) findViewById(R.id.bt1));
bt2 = ((Button) findViewById(R.id.bt2));
bt3 = ((Button) findViewById(R.id.bt3));
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
pb1 = ((ProgressBar) findViewById(R.id.pb1));
pb2 = ((ProgressBar) findViewById(R.id.pb2));
pb3 = ((ProgressBar) findViewById(R.id.pb3));
mc1 = new MyCount(30000, 1000);
mc1.setPb(bt1, pb1);
mc2 = new MyCount(30000, 1000);
mc2.setPb(bt2, pb2);
mc3 = new MyCount(30000, 1000);
mc3.setPb(bt3, pb3);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt1:
if (mc1Click) {
mc1.cancel();
pb1.setVisibility(View.GONE);
mc1Click = false;
} else {
pb1.setVisibility(View.VISIBLE);
mc1.start();
mc1Click = true;
}
break;
case R.id.bt2:
if (mc2Click) {
pb2.setVisibility(View.GONE);
mc2.cancel();
mc2Click = false;
} else {
pb2.setVisibility(View.VISIBLE);
mc2.start();
mc2Click = true;
}
break;
case R.id.bt3:
if (mc3Click) {
pb3.setVisibility(View.GONE);
mc3.cancel();
mc3Click = false;
} else {
pb3.setVisibility(View.VISIBLE);
mc3.start();
mc3Click = true;
}
break;
}
}
/*定義一個(gè)倒計(jì)時(shí)的內(nèi)部類*/
class MyCount extends CountDownTimer {
Button mBt;
ProgressBar mPb;
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void setPb(Button bt, ProgressBar pb) {
mBt = bt;
mPb = pb;
}
@Override
public void onFinish() {
mPb.setVisibility(View.GONE);
}
@Override
public void onTick(long millisUntilFinished) {
mBt.setText("請(qǐng)等待30秒(" + millisUntilFinished / 1000 + ")...");
Toast.makeText(MainActivity.this, millisUntilFinished / 1000 + "", Toast.LENGTH_LONG).show();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="cn.sh.changxing.countdowntest.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/bt1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="測(cè)試啟動(dòng)1"/>
<ProgressBar
android:id="@+id/pb1"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/bt2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="測(cè)試啟動(dòng)2"/>
<ProgressBar
android:id="@+id/pb2"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/bt3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="測(cè)試啟動(dòng)3"/>
<ProgressBar
android:id="@+id/pb3"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true"/>
</LinearLayout>
</LinearLayout>
以上所述是小編給大家介紹的Android 中不用線程如何實(shí)現(xiàn)倒計(jì)時(shí),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- android使用AsyncTask實(shí)現(xiàn)多線程下載實(shí)例
- android主線程和子線程之間消息傳遞詳解
- Android線程實(shí)現(xiàn)圖片輪播
- Android使用listview實(shí)現(xiàn)分頁(yè)刷新(線程休眠模擬)
- Android Socket 線程連接openwrt與arduino單片機(jī)串口雙向通信的實(shí)例解析
- Android 中通過(guò)實(shí)現(xiàn)線程更新Progressdialog (對(duì)話進(jìn)度條)
- Android實(shí)現(xiàn)網(wǎng)絡(luò)多線程斷點(diǎn)續(xù)傳下載實(shí)例
- Android 開發(fā)線程的分析
相關(guān)文章
Flutter給控件實(shí)現(xiàn)鉆石般的微光特效
這篇文章主要給大家介紹了關(guān)于Flutter給控件實(shí)現(xiàn)鉆石般的微光特效的相關(guān)資料,實(shí)現(xiàn)的效果非常不錯(cuò),非常適合大家做開發(fā)的時(shí)候參考,需要的朋友可以參考下2021-08-08
Android ViewPager制作新手導(dǎo)航頁(yè)(動(dòng)態(tài)加載)
這篇文章主要為大家詳細(xì)介紹了Android ViewPager制作新手導(dǎo)航頁(yè),了解什么是動(dòng)態(tài)加載指示器,感興趣的小伙伴們可以參考一下2016-05-05
Android實(shí)現(xiàn)簡(jiǎn)易記事本
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易記事本,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android 5.0及以上編程實(shí)現(xiàn)屏幕截圖功能的方法
這篇文章主要介紹了Android 5.0及以上編程實(shí)現(xiàn)屏幕截圖功能的方法,結(jié)合實(shí)例形式分析了Android5.0以上實(shí)現(xiàn)截圖功能的相關(guān)類、函數(shù)及權(quán)限控制等操作技巧,需要的朋友可以參考下2018-01-01
Android開發(fā)使用Activity嵌套多個(gè)Fragment實(shí)現(xiàn)橫豎屏切換功能的方法
這篇文章主要介紹了Android開發(fā)使用Activity嵌套多個(gè)Fragment實(shí)現(xiàn)橫豎屏切換功能的方法,結(jié)合實(shí)例形式分析了Android使用Activity嵌套多個(gè)Fragment進(jìn)行橫豎屏切換的原理與具體操作技巧,需要的朋友可以參考下2017-11-11
Android自定義扇形倒計(jì)時(shí)實(shí)例代碼
最近工作中需要做一個(gè)倒計(jì)時(shí),是那種一個(gè)圓,慢慢的被吃掉的動(dòng)畫倒計(jì)時(shí),由于自己是android小白,效果還不是多滿意,先給大家分享實(shí)例代碼,僅供大家參考2017-03-03
android使用ViewPager組件實(shí)現(xiàn)app引導(dǎo)查看頁(yè)面
這篇文章主要為大家詳細(xì)介紹了android使用ViewPager組件實(shí)現(xiàn)app引導(dǎo)查看頁(yè)面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07

