android實(shí)現(xiàn)倒計(jì)時(shí)功能(開始、暫停、0秒結(jié)束)
本文實(shí)例為大家分享了android實(shí)現(xiàn)倒計(jì)時(shí)功能的具體代碼,供大家參考,具體內(nèi)容如下
【思路】:通過 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="開始" 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("開始")){ //切換按鈕文字
button_start_timer.setText("暫停");
initView();
}
else{
button_start_timer.setText("開始");
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;
}
};
};
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Android利用RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí)效果
欄 目:Android
下一篇:Android ListView列表實(shí)現(xiàn)倒計(jì)時(shí)
本文標(biāo)題:android實(shí)現(xiàn)倒計(jì)時(shí)功能(開始、暫停、0秒結(jié)束)
本文地址:http://www.jygsgssxh.com/a1/Android/9188.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動登錄
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條


閱讀排行
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?


