Android實(shí)現(xiàn)倒計(jì)時(shí)30分鐘功能
以30分鐘為例寫(xiě)的一個(gè)倒計(jì)時(shí):
直接上代碼
public class MainActivity extends AppCompatActivity {
private int minute = 30;//這是分鐘
private int second = 0;//這是分鐘后面的秒數(shù)。這里是以30分鐘為例的,所以,minute是30,second是0
private TextView timeView;
private Timer timer;
private TimerTask timerTask;
//這是接收回來(lái)處理的消息
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (minute == 0) {
if (second == 0) {
timeView.setText("Time out !");
if (timer != null) {
timer.cancel();
timer = null;
}
if (timerTask != null) {
timerTask = null;
}
} else {
second--;
if (second >= 10) {
timeView.setText("0" + minute + ":" + second);
} else {
timeView.setText("0" + minute + ":0" + second);
}
}
} else {
if (second == 0) {
second = 59;
minute--;
if (minute >= 10) {
timeView.setText(minute + ":" + second);
} else {
timeView.setText("0" + minute + ":" + second);
}
} else {
second--;
if (second >= 10) {
if (minute >= 10) {
timeView.setText(minute + ":" + second);
} else {
timeView.setText("0" + minute + ":" + second);
}
} else {
if (minute >= 10) {
timeView.setText(minute + ":0" + second);
} else {
timeView.setText("0" + minute + ":0" + second);
}
}
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timeView = (TextView) findViewById(R.id.tv);
timeView.setText(minute + ":" + second);
timerTask = new TimerTask() {
@Override
public void run() {
Message msg = new Message();
msg.what = 0;
handler.sendMessage(msg);
}
};
timer = new Timer();
timer.schedule(timerTask, 0, 1000);
}
@Override
protected void onDestroy() {
if (timer != null) {
timer.cancel();
timer = null;
}
if (timerTask != null) {
timerTask = null;
}
minute = -1;
second = -1;
super.onDestroy();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onPause() {
super.onPause();
}
}
ok,這就完成了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android自定義倒計(jì)時(shí)控件示例
- android實(shí)現(xiàn)倒計(jì)時(shí)功能代碼
- Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的常用方法小結(jié)
- Android自定義圓形倒計(jì)時(shí)進(jìn)度條
- Android實(shí)現(xiàn)倒計(jì)時(shí)方法匯總
- Android啟動(dòng)頁(yè)面定時(shí)跳轉(zhuǎn)的三種方法
- Android利用CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能 Android實(shí)現(xiàn)停留5s跳轉(zhuǎn)到登錄頁(yè)面
- Android 實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
- Android使用Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
- Android實(shí)現(xiàn)倒計(jì)時(shí)結(jié)束后跳轉(zhuǎn)頁(yè)面功能
相關(guān)文章
Android camera2 判斷相機(jī)功能是否可控的實(shí)例
下面小編就為大家?guī)?lái)一篇Android camera2 判斷相機(jī)功能是否可控的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
android實(shí)現(xiàn)程序自動(dòng)升級(jí)到安裝示例分享(下載android程序安裝包)
這篇文章主要介紹了android實(shí)現(xiàn)下載android程序安裝包自動(dòng)升級(jí)的示例,大家參考使用吧2014-01-01
如何利用Android Studio將moudle變成jar示例詳解
這篇文章主要給大家介紹了關(guān)于如何利用Android Studio將moudle變成jar的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
android 開(kāi)發(fā) 文件讀寫(xiě)應(yīng)用案例分析
在Android應(yīng)用中保存文件會(huì)使用到文件讀寫(xiě)技術(shù),本文將詳細(xì)介紹,需要的朋友可以參考下2012-12-12
Android購(gòu)物車項(xiàng)目快速開(kāi)發(fā)
這篇文章主要為大家詳細(xì)介紹了Android購(gòu)物車項(xiàng)目快速開(kāi)發(fā),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android如何使用RecyclerView打造首頁(yè)輪播圖
這篇文章主要為大家詳細(xì)介紹了Android如何使用RecyclerView打造首頁(yè)輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
修改Android FloatingActionButton的title的文字顏色及背景顏色實(shí)例詳解
這篇文章主要介紹了修改Android FloatingActionButton的title的文字顏色及背景顏色實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03

