Android 秒表坏了
我可以看到线程的日期和时间运行。但是没有秒表的线索。有什么问题吗 公共类MainActivity扩展了活动{Android 秒表坏了,android,Android,我可以看到线程的日期和时间运行。但是没有秒表的线索。有什么问题吗 公共类MainActivity扩展了活动{ private Button Start, Reset, Stop; private EditText stop_watch, lblDate, lblTime; public MainActivity() { } private final UpdateClockThread ucThread = new UpdateClockThread(); private final St
private Button Start, Reset, Stop;
private EditText stop_watch, lblDate, lblTime;
public MainActivity() {
}
private final UpdateClockThread ucThread = new UpdateClockThread();
private final StopwatchThread swThread = new StopwatchThread();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Start = (Button)findViewById(R.id.button1);
Stop = (Button)findViewById(R.id.button3);
Reset = (Button)findViewById(R.id.button2);
stop_watch = (EditText)findViewById(R.id.editText3);
lblTime = (EditText)findViewById(R.id.editText2);
lblDate = (EditText)findViewById(R.id.editText1);
init();
Start.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
startactionPerformed();
}
});
Stop.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
stopactionPerformed();
}
});
Reset.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
resetactionPerformed();
}
});
}
public void init() {
swThread.setDaemon(true);
ucThread.setDaemon(true);
swThread.start(); // this doesent seem to wrk..
ucThread.start();
}
/** Listens to the Start/Stop/Resume button. */
void startactionPerformed() {
swThread.go();
}
void stopactionPerformed() {
swThread.noGo();
}
void resetactionPerformed() {
swThread.reset();
}
/** A thread that updates the current date & time. */
private class UpdateClockThread extends Thread {
/** The actual work of the thread. */
public void run() {
while (true) {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
Calendar now = Calendar.getInstance();
String month = Integer.toString(now.get(Calendar.MONTH)+1);
String date = Integer.toString(now.get(Calendar.DAY_OF_MONTH));
String year = Integer.toString(now.get(Calendar.YEAR));
String hour = Integer.toString(now.get(Calendar.HOUR));
if (hour.equals("0")) hour = "12";
String minute = Integer.toString(now.get(Calendar.MINUTE));
if (minute.length() == 1) minute = "0" + minute;
String second = Integer.toString(now.get(Calendar.SECOND));
if (second.length() == 1) second = "0" + second;
String ampm = now.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM";
lblDate.setText(month + "/" + date + "/" + year);
lblTime.setText(hour + ":" + minute + ":" + second + " " + ampm);
}
});
try {
sleep(500);
} catch (InterruptedException e) {}
}
}
}
/** A thread that keeps track of the stop watch & updates
* the display accordingly.
*/
class StopwatchThread extends Thread {
/** Whether or not stop watch is running. */
private boolean going = false;
/** Stores elapsed milliseconds of previous runs. */
private long prevElapsed = 0;
/** Stores beginning time of this run. */
private Date startDate = new Date();
/** Returns elapsed time in milliseconds.
*@return The elapsed time
*/
private long elapsedTime() {
return prevElapsed + (going ? new Date().getTime() - startDate.getTime() : 0);
}
/** Changes the number of elapsed milliseconds into a string.
*@param time Number of elapsed milliseconds
*@return The elapsed time as a string.
*/
private String msToString(long time) {
String ms, sec, min;
if (time % 10 >= 5) //round to nearest hundredth
time += 5;
ms = Long.toString(time % 1000);
while (ms.length() < 3)
ms = "0" + ms;
ms = ms.substring(0, ms.length() - 1);
time /= 1000;
sec = Long.toString(time % 60);
if (sec.length() == 1) sec = "0" + sec;
time /= 60;
min = Long.toString(time);
stop_watch.setText(min);
return min + ":" + sec + "." + ms;
}
/** Called when the stop watch is to go.
*/
public void go() {
startDate = new Date();
going = true;
}
/** Called when the stop watch is to stop.
*/
public void noGo() {
prevElapsed = elapsedTime();
going = false;
}
/** Resets the stop watch.
*/
public void reset() {
going = false;
prevElapsed = 0;
}
/** Adds a lap to the list.
*/
/** Main code of the thread.
*/
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
stop_watch.setText(msToString(elapsedTime()));
}
});
yield();
}
}
专用按钮启动、复位、停止;
私人编辑文本秒表,lblDate,lblTime;
公共活动(){
}
private final UpdateLockThread ucThread=新UpdateLockThread();
私有最终StopwatchThread swThread=新StopwatchThread();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
开始=(按钮)findViewById(R.id.button1);
停止=(按钮)findViewById(R.id.button3);
重置=(按钮)findViewById(R.id.button2);
stop_watch=(EditText)findViewById(R.id.editText3);
lblTime=(EditText)findViewById(R.id.editText2);
lblDate=(EditText)findViewById(R.id.editText1);
init();
Start.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v)
{
startactionPerformed();
}
});
Stop.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v)
{
stopactionPerformed();
}
});
Reset.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v)
{
resetactionPerformed();
}
});
}
公共void init(){
setDaemon(true);
setDaemon(true);
swThread.start();//这似乎有问题。。
ucThread.start();
}
/**收听开始/停止/恢复按钮*/
void startactionPerformed(){
swThread.go();
}
void stopactionPerformed(){
swThread.noGo();
}
void resetactionPerformed(){
swThread.reset();
}
/**更新当前日期和时间的线程*/
私有类UpdateLockThread扩展线程{
/**线程的实际工作*/
公开募捐{
while(true){
MainActivity.this.runOnUiThread(新的Runnable(){
公开募捐{
Calendar now=Calendar.getInstance();
字符串month=Integer.toString(now.get(Calendar.month)+1);
String date=Integer.toString(now.get(Calendar.DAY/u/u MONTH));
字符串year=Integer.toString(now.get(Calendar.year));
字符串hour=Integer.toString(now.get(Calendar.hour));
如果(小时等于(“0”)小时=“12”;
字符串minute=Integer.toString(now.get(Calendar.minute));
如果(分钟长度()==1)分钟=“0”+分钟;
String second=Integer.toString(now.get(Calendar.second));
如果(second.length()==1)second=“0”+秒;
字符串ampm=now.get(Calendar.AM\u PM)==Calendar.AM?“AM”:“PM”;
lblDate.setText(月+“/”+日期+“/”+年);
lblTime.setText(小时+“:“+分钟+”:“+秒+”+ampm);
}
});
试一试{
睡眠(500);
}捕获(中断异常e){}
}
}
}
/**跟踪秒表和更新的线程
*相应地,显示器也会显示。
*/
类StopwatchThread扩展线程{
/**秒表是否在运行*/
私有布尔值=false;
/**存储以前运行的已用毫秒数*/
私用长时间=0;
/**存储此运行的开始时间*/
私有日期开始日期=新日期();
/**返回以毫秒为单位的已用时间。
*@返回经过的时间
*/
私有长延时(){
返回Previoused+(正在进行?新日期().getTime()-startDate.getTime():0);
}
/**将已用毫秒数更改为字符串。
*@参数时间已用毫秒数
*@以字符串形式返回经过的时间。
*/
私有字符串msToString(长时间){
字符串ms,sec,min;
if(时间%10>=5)//四舍五入到最接近的百分之一
时间+=5;
ms=长时间串(时间%1000);
while(ms.length()<3)
ms=“0”+ms;
ms=ms.substring(0,ms.length()-1);
时间/=1000;
秒=长时间串(时间%60);
如果(秒长()==1)秒=0“+秒;
时间/=60;
最小值=长到串(时间);
秒表设置文本(最小值);
返回min+“:“+sec+”+ms;
}
/**当秒表要走的时候叫。
*/
公开作废go(){
startDate=新日期();
继续=正确;
}
/**当秒表要停的时候叫。
*/
公共图书馆{
prevassed=elapsedTime();
去=假;
}
/**重置秒表。
*/
公共无效重置(){
去=假;
预测值=0;
}
/**将圈数添加到列表中。
*/
/**线程的主代码。
*/
公开募捐{
MainActivity.this.runOnUiThread(新的Runnable(){
公开募捐{
stop_watch.setText(msToString(elapsedTime());
}
});
收益率();
}
}
}
我可以看到线程的日期和时间运行。但是没有秒表的线索。有什么问题吗
我如何启动秒表线程。。请帮忙
我的程序中有两个守护进程线程。。但日志显示只有一个处于活动状态。。即使我终止活动线程。秒表还是不响。我试了很多。但是我不知道问题出在哪里
DalvikVM[localhost:8633]
线程[主](正在运行)
螺纹[活页夹螺纹#2](运行)
螺纹[活页夹螺纹#1](运行)
守护进程线程[Thread-10](正在运行)问题在于StopWatch线程的
run()
方法
它在UI线程上运行单个任务(调用stop\u watch.setText()
),然后停止。
你没有任何形式的循环,即使你