欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android實(shí)現(xiàn)倒計(jì)時(shí)功能(開始、暫停、0秒結(jié)束)

 更新時(shí)間:2019年09月20日 15:42:45   作者:WillianF  
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)倒計(jì)時(shí)功能,開始、暫停、0秒結(jié)束,具有一定的參考價(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論