android實(shí)現(xiàn)倒計(jì)時(shí)功能(開(kāi)始、暫停、0秒結(jié)束)
本文實(shí)例為大家分享了android實(shí)現(xiàn)倒計(jì)時(shí)功能的具體代碼,供大家參考,具體內(nèi)容如下
【思路】:通過(guò) timer 執(zhí)行周期延時(shí)的任務(wù),handler 中將計(jì)時(shí)信息更新,并在計(jì)時(shí)結(jié)束時(shí)結(jié)束 timer 的周期任務(wù)。
- 在布局文件中添加一個(gè)TextView和Button控件,并在onCreate方法中獲得到TextView和Button的id;
xml布局代碼:
<Button android:id="@+id/button_start_timer" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" android:gravity="center" android:text="開(kāi)始" android:textSize="12sp" /> <TextView android:id="@+id/textViewTime24" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="2" android:gravity="center" android:text="24" android:textColor="#33ff00" android:textSize="60sp" />
java代碼
package com.example.wlf.gamerecorder.gameon;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.os.Handler;
import com.example.wlf.gamerecorder.R;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class SimpleGameonActivity extends AppCompatActivity {
private final static int COUNT = 1;
private final static int TOTAL_TIME_24 = 24;
private TextView textViewTime24;
Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_gameon);
textViewTime24=(TextView)findViewById(R.id.textViewTime24);//24秒倒計(jì)時(shí)
final Button button_start_timer = (Button)findViewById(R.id.button_start_timer);
button_start_timer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = button_start_timer.getText().toString();//獲取按鈕字符串
if(str.equals("開(kāi)始")){ //切換按鈕文字
button_start_timer.setText("暫停");
initView();
}
else{
button_start_timer.setText("開(kāi)始");
timer.cancel();//終止線程
}
}
});
}
public void initView(){
//countDown = (TextView) findViewById(R.id.textViewTime24);
timer = new Timer();
/**
* 每一秒發(fā)送一次消息給handler更新UI
* schedule(TimerTask task, long delay, long period)
*/
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(COUNT);
}
}, 0, 1000);
}
private Handler handler = new Handler(){
int num = TOTAL_TIME_24;
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case COUNT:
textViewTime24.setText(String.valueOf(num));
if(num == 0)
timer.cancel();//0秒結(jié)束
num--;
break;
default:
break;
}
};
};
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android計(jì)時(shí)與倒計(jì)時(shí)實(shí)現(xiàn)限時(shí)搶購(gòu)的5種方法
- Android限時(shí)搶購(gòu)倒計(jì)時(shí)實(shí)現(xiàn)代碼
- android實(shí)現(xiàn)圓環(huán)倒計(jì)時(shí)控件
- android利用handler實(shí)現(xiàn)倒計(jì)時(shí)功能
- Android自定義view實(shí)現(xiàn)倒計(jì)時(shí)控件
- Android實(shí)現(xiàn)倒計(jì)時(shí)效果
- 解決Android-RecyclerView列表倒計(jì)時(shí)錯(cuò)亂問(wèn)題
- Android實(shí)現(xiàn)自定義倒計(jì)時(shí)
- Android 倒計(jì)時(shí)控件 CountDownView的實(shí)例代碼詳解
- Android倒計(jì)時(shí)神器(CountDownTimer)
- Android 簡(jiǎn)單實(shí)現(xiàn)倒計(jì)時(shí)功能
- Android 實(shí)現(xiàn)搶購(gòu)倒計(jì)時(shí)功能的示例
相關(guān)文章
Android開(kāi)發(fā)實(shí)現(xiàn)可拖動(dòng)排序的ListView功能【附源碼下載】
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)可拖動(dòng)排序的ListView功能,結(jié)合實(shí)例形式分析了Android列表拖動(dòng)排序原理與相關(guān)操作技巧,并附帶完整源碼供讀者下載參考,需要的朋友可以參考下2017-11-11
Android 控制ScrollView滾動(dòng)的實(shí)例詳解
這篇文章主要介紹了Android 控制ScrollView滾動(dòng)的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-10-10
Android調(diào)用攝像頭拍照開(kāi)發(fā)教程
這篇文章主要為大家詳細(xì)介紹了Android調(diào)用攝像頭拍照的開(kāi)發(fā)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Android開(kāi)發(fā)使用自定義view實(shí)現(xiàn)ListView下拉的視差特效功能
這篇文章主要介紹了Android開(kāi)發(fā)使用自定義view實(shí)現(xiàn)ListView下拉的視差特效功能,結(jié)合實(shí)例形式詳細(xì)分析了Android重寫(xiě)ListView控件實(shí)現(xiàn)ListView下拉視差效果的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Android編程實(shí)現(xiàn)設(shè)置TabHost當(dāng)中字體的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)設(shè)置TabHost當(dāng)中字體的方法,涉及Android針對(duì)TabHost屬性操作的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-12-12
Android列表實(shí)現(xiàn)單選點(diǎn)擊縮放動(dòng)畫(huà)效果
在android開(kāi)發(fā),我們會(huì)常常使用到縮放動(dòng)畫(huà),這篇文章主要給大家介紹了關(guān)于Android列表實(shí)現(xiàn)單選點(diǎn)擊縮放動(dòng)畫(huà)效果的相關(guān)資料,需要的朋友可以參考下2021-08-08
Android實(shí)現(xiàn)定制返回按鈕動(dòng)畫(huà)效果的方法
這篇文章主要介紹了Android實(shí)現(xiàn)定制返回按鈕動(dòng)畫(huà)效果的方法,涉及Android控件及動(dòng)畫(huà)的相關(guān)操作技巧,需要的朋友可以參考下2016-02-02
Android編程實(shí)現(xiàn)向桌面添加快捷方式的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)向桌面添加快捷方式的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android添加桌面快捷方式的操作技巧,需要的朋友可以參考下2016-11-11

