Android中的倒计时的功能(二)

Android中的倒计时的功能(也可以直接使用CountDownTimer这个类直接实现,相关此Demo可查看我的博客),参考了网上写的很好的一个倒计时Demo:


下面提供下代码,和大家一起分享一下:

MainActivity:

  1. package com.example.mytime;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.Window;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13.   
  14. public class MainActivity extends Activity {  
  15.   
  16.     Button startButton;  
  17.     EditText minuteText;  
  18.     EditText secondText;  
  19.     int minute;  
  20.     int second;  
  21.       
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  26.         setContentView(R.layout.main);  
  27.   
  28.         startButton = (Button) findViewById(R.id.button_start);  
  29.         minuteText = (EditText)findViewById(R.id.minute);  
  30.         secondText = (EditText)findViewById(R.id.second);  
  31.           
  32.         startButton.setOnClickListener(new OnClickListener() {  
  33.   
  34.             @Override  
  35.             public void onClick(View v) {  
  36.                 if (!minuteText.getText().toString().equals("")) {  
  37.                     minute = Integer.parseInt(minuteText.getText().toString());  
  38.                 }  
  39.                 if (!secondText.getText().toString().equals("")) {  
  40.                     second = Integer.parseInt(secondText.getText().toString());  
  41.                 }  
  42.                 if (minute != 0 || second != 0) {  
  43.                       
  44.                     ArrayList<Integer> list = new ArrayList<Integer>();  
  45.                     list.add(minute);  
  46.                     list.add(second);  
  47.                       
  48.                     Intent intent = new Intent();  
  49.                     intent.setAction("com.example.mytime.StartActivity");  
  50.                       
  51.                     intent.putIntegerArrayListExtra("times", list);  
  52.                     startActivity(intent);  
  53.                 }  
  54.             }  
  55.         });  
  56.     }  
  57.       
  58.     @Override  
  59.     protected void onResume() {  
  60.         minute = 0;  
  61.         second = 0;  
  62.         super.onResume();  
  63.     }  
  64. }  

具体倒计时相关类 StartActivity

  1. package com.example.mytime;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Timer;  
  5. import java.util.TimerTask;  
  6.   
  7. import android.app.Activity;  
  8. import android.content.Intent;  
  9. import android.os.Bundle;  
  10. import android.os.Handler;  
  11. import android.os.Message;  
  12. import android.view.Window;  
  13. import android.widget.TextView;  
  14.   
  15. public class StartActivity extends Activity{  
  16.   
  17.     static int minute = -1;  
  18.     static int second = -1;  
  19.     final static String tag = "tag";  
  20.     TextView timeView;  
  21.     Timer timer;  
  22.     TimerTask  timerTask;  
  23.     Handler handler = new Handler(){  
  24.         public void handleMessage(Message msg) {  
  25.             System.out.println("handle!");  
  26.             if (minute == 0) {  
  27.                 if (second == 0) {  
  28.                     timeView.setText("Time out !");  
  29.                     if (timer != null) {  
  30.                         timer.cancel();  
  31.                         timer = null;  
  32.                     }  
  33.                     if (timerTask != null) {  
  34.                         timerTask = null;  
  35.                     }  
  36.                 }else {  
  37.                     second--;  
  38.                     String minutes = String.valueOf(minute);  
  39.                     String minute = minutes;  
  40.                     minute = "";  
  41.                     if (second >= 10) {  
  42.                         minutes = String.valueOf(minute);  
  43.                         minute = minutes;  
  44.                         minute = "";  
  45.                         timeView.setText(minute + second);/*"0"+minute + ":" + */  
  46.                     }else {  
  47.                         timeView.setText(minute + "0" + second);/*"0"+minute + ":0" + */  
  48.                     }  
  49.                 }  
  50.             }else {  
  51.                 if (second == 0) {  
  52.                     second =59;  
  53.                     minute--;  
  54.                     if (minute >= 10) {  
  55.                         timeView.setText(minute + ":" + second);  
  56.                     }else {  
  57.                         timeView.setText("0"+minute + ":" + second);  
  58.                     }  
  59.                 }else {  
  60.                     second--;  
  61.                     if (second >= 10) {  
  62.                         if (minute >= 10) {  
  63.                             timeView.setText(minute + ":" + second);  
  64.                         }else {  
  65.                             timeView.setText("0"+minute + ":" + second);  
  66.                         }  
  67.                     }else {  
  68.                         if (minute >= 10) {  
  69.                             timeView.setText(minute + ":0" + second);  
  70.                         }else {  
  71.                             timeView.setText("0"+minute + ":0" + second);  
  72.                         }  
  73.                     }  
  74.                 }  
  75.             }  
  76.         };  
  77.     };  
  78.       
  79.       
  80.     @Override  
  81.     protected void onCreate(Bundle savedInstanceState) {  
  82.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  83.         super.onCreate(savedInstanceState);  
  84.         setContentView(R.layout.start);  
  85.         timeView = (TextView)findViewById(R.id.myTime);  
  86.           
  87.         if (minute == -1 && second == -1) {  
  88.             Intent intent = getIntent();  
  89.             ArrayList<Integer> times = intent.getIntegerArrayListExtra("times");  
  90.             minute = times.get(0);  
  91.             second = times.get(1);  
  92.         }  
  93.           
  94.         timeView.setText(minute + ":" + second);  
  95.           
  96.         timerTask = new TimerTask() {  
  97.               
  98.             @Override  
  99.             public void run() {  
  100.                 Message msg = new Message();  
  101.                 msg.what = 0;  
  102.                 handler.sendMessage(msg);  
  103.             }  
  104.         };  
  105.           
  106.         timer = new Timer();  
  107.         timer.schedule(timerTask,0,1000);  
  108.           
  109.     }  
  110.       
  111.     @Override  
  112.     protected void onDestroy() {  
  113.         if (timer != null) {  
  114.             timer.cancel();  
  115.             timer = null;  
  116.         }  
  117.         if (timerTask != null) {  
  118.             timerTask = null;  
  119.         }  
  120.         minute = -1;  
  121.         second = -1;  
  122.         super.onDestroy();  
  123.     }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值