編輯:關於Android編程
需求:
有多個組件可以開啟倒計時,正常情況下默認倒計時時間終了後更新UI,另,用戶可以取消指定倒計時。
這裡使用CountDownTimer進行倒計時,其中回調函數onFinish是在倒計時終了時回調,onTick是在倒計時開始時回調,用戶可以使用CountDownTimer對象的cancel方法取消倒計時。
這樣做的好處:不需要使用繁瑣的線程去控制倒計時,更方便的進行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;
}
}
/*定義一個倒計時的內部類*/
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("請等待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="測試啟動1"/>
<ProgressBar
android:id="@+id/pb1"
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="測試啟動2"/>
<ProgressBar
android:id="@+id/pb2"
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="測試啟動3"/>
<ProgressBar
android:id="@+id/pb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true"/>
</LinearLayout>
</LinearLayout>
以上所述是小編給大家介紹的Android 中不用線程如何實現倒計時,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!
Android優雅的方式解決軟鍵盤遮擋按鈕問題
前言比如在進行登錄的操作中,用戶輸入完密碼之後,肯定是想直接點擊登錄按鈕的。返回鍵隱藏軟鍵盤這樣的體驗肯定很糟糕,程序員,遇到問題解決問題。實現1xml<Scrol
Android中使用ShareSDK功能學習
前言:由於最近做了個項目用到了社會化分享的功能,之前從來沒有碰到過這類功能,然後自己就一邊查看資料,一邊在項目中加入,慢慢摸索,這邊文章算是自己對ShareSDK的學習筆
Android開發之OkHttp的使用
本篇記錄的是Android開發中OkHttp框架的使用,下面介紹OkHttp庫的用法,本篇會給出OkHttp的使用demo,demo中包含了常用的get請求、post請求
android開發教程之使用listview顯示qq聯系人列表
首先還是xml布局文件,在其中添加ListView控件:主布局layout_main.xml復制代碼 代碼如下:<RelativeLayout xmlns:andr