上面一个编辑框输入要倒计时的数字
获取以后显示在下面的textview上面
点击开始,textview上的数字逐减
private TextView tvTime; private EditText inputTv; private Button btnHQ,btnBG,btnZT; //用来存放倒计时的数字 private int i; private Timer timer; private TimerTask task;public void onClick(View v) {switch (v.getId()) {case R.id.button1:String time = inputTv.getText().toString();tvTime.setText(time);i = Integer.parseInt(time);break;case R.id.button2:start();break;case R.id.button3:timer.cancel();break;}}
Handler mHandler = new Handler(){ public void handleMessage(android.os.Message msg) { tvTime.setText(msg.arg1+""); //等于零的时候不在自减 if(msg.arg1==0){ timer.cancel(); }else{ start(); } }; };
private void start() { timer = new Timer(); task = new TimerTask() { @Override public void run() { i--; Message msg = mHandler.obtainMessage(); //将i放入msg里面 msg.arg1 = i; mHandler.sendMessage(msg); } }; //一定要记得部署这个方法 timer.schedule(task, 1000); }