如图是效果图其中2个文本文件是获取当前的系统时间其中最关键的是计时器的代码设置如下是计时器的代码和布局文件 1 package org. 2 3 import java.util.Timer; 4 import java.util.TimerTask; 5 6 import ...
如图是效果图
其中2个文本文件是获取当前的系统时间
其中最关键的是计时器的代码设置
如下是计时器的代码和布局文件
1 package org. 2 3 import java.util.Timer; 4 import java.util.TimerTask; 5 6 import ogg.huanxin.huadong.R; 7 import android.content.Context; 8 import android.os.Handler; 9 import android.util.AttributeSet; 10 import android.view.LayoutInflater; 11 import android.view.View; 12 import android.widget.LinearLayout; 13 import android.widget.TextView; 14 import android.widget.Toast; 15 16 public class TimeView extends LinearLayout { 17 // 小时的十位数 18 private TextView tv_hour_decade; 19 // 小时的个位数 20 private TextView tv_hour_unit; 21 // 分钟的十位数 22 private TextView tv_min_decade; 23 // 分钟的个位数 24 private TextView tv_min_unit; 25 // 秒的十位数 26 private TextView tv_sec_decade; 27 // 秒的个位数 28 private TextView tv_sec_unit; 29 30 private Context context; 31 private int hour_decade; 32 private int hour_unit; 33 private int min_decade; 34 private int min_unit; 35 private int sec_decade; 36 private int sec_unit; 37 38 // 计时器 39 private Timer timer; 40 private Handler handler = new Handler() { 41 public void handleMessage(android.os.Message msg) { 42 countdown(); 43 } 44 45 }; 46 47 public TimeView(Context context, AttributeSet attrs) { 48 super(context, attrs); 49 // TODO Auto-generated constructor stub 50 this.context = context; 51 52 LayoutInflater inflater = (LayoutInflater) context 53 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 54 View view = inflater.inflate(R.layout.jishidemo, this); 55 tv_hour_decade = (TextView) view.findViewById(R.id.tv_hour_decade); 56 tv_hour_unit = (TextView) view.findViewById(R.id.tv_hour_unit); 57 tv_min_decade = (TextView) view.findViewById(R.id.tv_min_decade); 58 tv_min_unit = (TextView) view.findViewById(R.id.tv_min_unit); 59 tv_sec_decade = (TextView) view.findViewById(R.id.tv_sec_decade); 60 tv_sec_unit = (TextView) view.findViewById(R.id.tv_sec_unit); 61 62 } 63 64 /** 65 * 开始倒计时 66 */ 67 public void start() { 68 if (timer == null) { 69 timer = new Timer(); 70 timer.schedule(new TimerTask() { 71 72 @Override 73 public void run() { 74 // TODO Auto-generated method stub 75 handler.sendEmptyMessage(0); 76 } 77 }, 0, 1000); 78 } 79 } 80 81 /** 82 * 结束计时 83 */ 84 public void stop() { 85 if (timer != null) { 86 timer.cancel(); 87 timer = null; 88 } 89 } 90 91 /** 92 * 设置时间的参数 93 */ 94 public void setTime(int hour, int min, int sec) { 95 if (hour >= 60 || min >= 60 || sec >= 60 || hour < 0 || min < 0 96 || sec < 0) { 97 throw new RuntimeException("Time format is error"); 98 } 99 100 hour_decade = hour / 10;101 hour_unit = hour - hour_decade * 10;102 103 min_decade = min / 10;104 min_unit = min - min_decade * 10;105 106 sec_decade = sec / 10;107 sec_unit = sec - sec_decade * 10;108 tv_hour_decade.setText(hour_decade + "");109 tv_hour_unit.setText(hour_unit + "");110 tv_min_decade.setText(min_decade + "");111 tv_min_unit.setText(min_unit + "");112 tv_sec_decade.setText(sec_decade + "");113 tv_sec_unit.setText(sec_unit + "");114 115 }116 117 /**118 * 变化时间的十位数119 * 120 * @param tv121 * @return boolean122 */123 private boolean isCarry4Decade(TextView tv) {124 int time1 = Integer.valueOf(tv.getText().toString());125 time1 = time1 - 1;126 if (time1 < 0) {127 time1 = 5;128 tv.setText(time1 + "");129 return true;130 } else {131 tv.setText(time1 + "");132 return false;133 }134 }135 136 private boolean isCarry4Unit(TextView tv) {137 138 int time = Integer.valueOf(tv.getText().toString());139 time = time - 1;140 if (time < 0) {141 time = 9;142 tv.setText(time + "");143 return true;144 } else {145 tv.setText(time + "");146 return false;147 }148 }149 150 /**151 * 倒计时152 */153 private void countdown() {154 if (isCarry4Unit(tv_sec_unit)) {155 if (isCarry4Decade(tv_sec_decade)) {156 157 if (isCarry4Unit(tv_min_unit)) {158 if (isCarry4Decade(tv_min_decade)) {159 160 if (isCarry4Unit(tv_hour_unit)) {161 if (isCarry4Decade(tv_hour_decade)) {162 Toast.makeText(context, "时间到了",163 Toast.LENGTH_SHORT).show();164 stop();165 }166 }167 }168 }169 }170 }171 }172 }
原标题:安卓定时器
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。