标签:
开篇大家可以先看看Timer的基础用法,以及简单的原理。http://my.oschina.net/zhengweishan/blog/493891 Java之Timer使用。这里我要说的是Android使用Timer编写一个倒计时程序。
需求:实现简单的倒计时程序。要求可以根据用户的输入实现倒计时,时间到的时候有友好的提示。
分析:首先要实现这个功能,我想到的第一个方法就是使用Timer这个类。然后就是分析用户界面怎么设计,由于这个是简单程序,所以就采用Android原生的一些UI组件Button TextView EditText来设计基本的页面。友好提示我打算使用Toast这种机制。这里打算让用户输入分秒。
源码下载地址:https://git.oschina.net/zhengweishan/AndroidTimer
Activity代码:
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button startButton;
private Button stopButton;
private EditText minuteText;
private EditText secondText;
private TextView myTime;
private int minute = 0;
private int second = 0;
private Timer timer = null;
private TimerTask task = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
startButton = (Button) findViewById(R.id.startBtn);
stopButton = (Button) findViewById(R.id.stopBtn);
minuteText = (EditText) findViewById(R.id.minute);
secondText = (EditText) findViewById(R.id.second);
myTime = (TextView) findViewById(R.id.myTime);
startButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.startBtn:
start();
break;
case R.id.stopBtn:
stop();
break;
}
}
public void start() {
if (!minuteText.getText().toString().equals("")) {
minute = Integer.parseInt(minuteText.getText().toString());
}
if (!secondText.getText().toString().equals("")) {
second = Integer.parseInt(secondText.getText().toString());
}
myTime.setText(minute + ":" + second);
task = new TimerTask() {
@Override
public void run() {
Message msg = new Message();
msg.what = 0;
mHandler.sendMessage(msg);
}
};
timer = new Timer();
timer.schedule(task, 0, 1000);
}
public void stop() {
timer.cancel();
}
public void timeProcess(){
if (minute == 0) {
if (second == 0) {
//myTime.setText("Time out!");
Toast.makeText(getApplicationContext(), "Time out!",5);
if (timer != null) {
timer.cancel();
timer = null;
}
if (task != null) {
task = null;
}
} else {
second--;
if (second >= 10) {
myTime.setText("0" + minute + ":" + second);
} else {
myTime.setText("0" + minute + ":0" + second);
}
}
} else {
if (second == 0) {
second = 59;
minute--;
if (minute >= 10) {
myTime.setText(minute + ":" + second);
} else {
myTime.setText("0" + minute + ":" + second);
}
} else {
second--;
if (second >= 10) {
if (minute >= 10) {
myTime.setText(minute + ":" + second);
} else {
myTime.setText("0" + minute + ":" + second);
}
} else {
if (minute >= 10) {
myTime.setText(minute + ":0" + second);
} else {
myTime.setText("0" + minute + ":0" + second);
}
}
}
}
}
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
timeProcess();
};
};
}
xml布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidtimer.MainActivity" >
<TextView
android:id="@+id/lable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/lable" />
<EditText
android:id="@+id/minute"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/lable"
android:layout_below="@+id/lable"
android:layout_marginTop="40dp"
android:ems="10" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/minute"
android:layout_toRightOf="@+id/minute"
android:text="@string/minute" />
<EditText
android:id="@+id/second"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textView2"
android:layout_below="@+id/lable"
android:layout_marginTop="40dp"
android:ems="10" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/second"
android:layout_alignBottom="@+id/second"
android:text="@string/second" />
<TextView
android:id="@+id/myTime"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/startBtn"
android:layout_alignRight="@+id/textView3"
android:layout_margin="30dp"
android:gravity="center"
android:textColor="#FF0000"
android:textSize="60sp"
android:textStyle="bold" />
<Button
android:id="@+id/startBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/minute"
android:layout_alignParentBottom="true"
android:text="@string/startBtn" />
<Button
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/startBtn"
android:layout_toRightOf="@+id/startBtn"
android:layout_alignRight="@+id/textView3"
android:layout_marginRight="40dp"
android:text="@string/stopBtn" />
</RelativeLayout>
string.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AndroidTimer</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="lable">请设置时间:</string>
<string name="minute">分:</string>
<string name="second">秒</string>
<string name="startBtn">开始计时</string>
<string name="stopBtn">停止计时</string>
</resources>
标签:
原文地址:http://my.oschina.net/zhengweishan/blog/498771