package Y.B;import java.util.ArrayList;
import java.util.List;
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.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;public class stopwatchActivity extends Activity implements OnClickListener {
RadioGroup rg;
RadioButton rbfenbei, rbyiquan;
ListView lvjilu;
TextView tvxianshi;
Button btnjishi, btnkaishi;
Timer timer = new Timer(); TimerTask task = new TimerTask() {
@Override
public void run() {
try {
Message msg = handler.obtainMessage();
msg.what = 0;
run[3]++;
if(run[3] == 100) {
run[3] = 0;
run[2]++;
}
if(run[2] == 60) {
run[2] = 0;
run[1]++;
}
if(run[1] == 60) {
run[1] = 0;
run[0]++;
}
Thread.sleep(10);
msg.obj = run[3];
msg.sendToTarget();
}
catch(Exception e) {
e.printStackTrace();
}
}
};
List<String> jilu = new ArrayList<String>();
ArrayAdapter<String> jilus;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what) {
case 0:// 显示时间
tvxianshi.setText((run[0] < 10 ? "0" + run[0] : run[0])
+ ":" + (run[1] < 10 ? "0" + run[1] : run[1]) + ":"
+ (run[2] < 10 ? "0" + run[2] : run[2]) + ":"
+ (run[3] < 10 ? "0" + run[3] : run[3]));
break;
}
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stopwatch);
rg = (RadioGroup) findViewById(R.id.rg);
rbfenbei = (RadioButton) findViewById(R.id.rbfenbie);
rbyiquan = (RadioButton) findViewById(R.id.rbyiquan);
lvjilu = (ListView) findViewById(R.id.lvjilu);
tvxianshi = (TextView) findViewById(R.id.tvxianshi);
btnjishi = (Button) findViewById(R.id.btnjishi);
btnkaishi = (Button) findViewById(R.id.btnkaishi);
btnkaishi.setOnClickListener(this);
btnjishi.setOnClickListener(this);
jilus = new ArrayAdapter<String>(stopwatchActivity.this,
android.R.layout.simple_list_item_1, jilu);
lvjilu.setAdapter(jilus);
} boolean isRun = false;// 是否在计时
int[] run = { 0, 0, 0, 0 };// 表示时间的时、分、秒、毫秒 @Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btnkaishi:
if(!isRun) {
timer.schedule(task, 10, 10);
isRun = true;
}else{}
break;
case R.id.btnjishi:
int[] one = run.clone();
String myjilu = (one[0] < 10 ? "0" + one[0] : one[0]) + ":"
+ (one[1] < 10 ? "0" + one[1] : one[1]) + ":"
+ (one[2] < 10 ? "0" + one[2] : one[2]) + ":"
+ (one[3] < 10 ? "0" + one[3] : one[3]);
jilu.add(myjilu);
jilus.notifyDataSetChanged();
break;
}
}
}我想写个秒表程序
我这样写了后运行2分钟里就有近20秒的误差
请问如果想做这样的一个程序应该怎么写?
解决方案 »
- Nexus 7平板上ZXing不能识别针式或喷墨打印机的条码.能换成条码枪用吗?
- 怎样可以做到多级分组
- 不使用google map api,如何实现overlay和地图上的气泡标记
- 请问android代码的混淆器有哪些?
- 关于安卓相册。
- 跪求Android中ListView设计原理分析
- 如何在ABC extends Activity中使用System.out.println在控制台Console
- 大家有Android自定义控件方面的资料吗?
- 请教一下如果写的app安装到手机上,那个图标要整好几个大小吗?
- sos android 传值问题
- 做安卓系统移植该看源码中哪些文件夹
- android怎么将一个文本文件写入pc机
timer回调仅仅是为了刷新
private boolean stoped;
private long start;
private long end;
private Handler handler = new Handler() { @Override
public void handleMessage(Message msg) {
refresh();
}
};private Thread thread;public void start() {
start = System.currentTimeMillis();
stoped = false;
(thread = new Thread() {
@Override
public void run() {
while (true) {
handler.sendEmptyMessage(0);
try {
sleep(10);
} catch (InterruptedException e) {
break;
}
}
}
}).start();
}public void stop() {
if (thread != null) {
end = System.currentTimeMillis();
stoped = true;
thread.interrupt();
thread = null;
}
}public void refresh() {
long time;
if(stoped)
{
time = end - start;
}
else
{
time = System.currentTimeMillis() - start;
}
//
//将毫秒时间 time 转为合适的格式,显示到界面上
//
}