当前位置:  开发笔记 > 编程语言 > 正文

停止观察逻辑

如何解决《停止观察逻辑》经验,为你挑选了2个好方法。

我想在android中开发一个简单的秒表逻辑.

单击列表视图时,计时器应该启动,单击按钮时计时器应该停止.任何人都可以指导我.任何示例代码都会有很大帮助



1> st0le..:

使用 秒表类(为了更高精度的使用System.nanoTime())

在Button Presses上添加Start()事件和Stop()事件.您需要更新UI,以便使用线程/处理程序组合.

这应该让你开始.

编辑:添加代码.(很棒的运动!:))

使用它Refresh_Rate来配置UI的更新频率.

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.TextView;

public class Main extends Activity implements OnClickListener{

    final int MSG_START_TIMER = 0;
    final int MSG_STOP_TIMER = 1;
    final int MSG_UPDATE_TIMER = 2;

    Stopwatch timer = new Stopwatch();
    final int REFRESH_RATE = 100;

    Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case MSG_START_TIMER:
                timer.start(); //start timer
                mHandler.sendEmptyMessage(MSG_UPDATE_TIMER);
                break;

            case MSG_UPDATE_TIMER:
                tvTextView.setText(""+ timer.getElapsedTime());
                mHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIMER,REFRESH_RATE); //text view is updated every second, 
                break;                                  //though the timer is still running
            case MSG_STOP_TIMER:
                mHandler.removeMessages(MSG_UPDATE_TIMER); // no more updates.
                timer.stop();//stop timer
                tvTextView.setText(""+ timer.getElapsedTime());
                break;

            default:
                break;
            }
        }
    };

    TextView tvTextView;
    Button btnStart,btnStop;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tvTextView = (TextView)findViewById(R.id.TextView01);

        btnStart = (Button)findViewById(R.id.Button01);
        btnStop= (Button)findViewById(R.id.Button02);
        btnStart.setOnClickListener(this);
        btnStop.setOnClickListener(this);

    }

    public void onClick(View v) {
        if(btnStart == v)
        {
            mHandler.sendEmptyMessage(MSG_START_TIMER);
        }else
        if(btnStop == v){
            mHandler.sendEmptyMessage(MSG_STOP_TIMER);
        }
    }
}


你的秒表链接已经死了

2> Bilal Rabban..:

正如st0le通过使用Stopwatch类给出了一个很好的例子.我稍微修改了这个类,并添加了一些方法.

/*
Copyright (c) 2005, Corey Goldberg

StopWatch.java is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

Modified: Bilal Rabbani bilalrabbani1@live.com (Nov 2013)
*/

package bilalrabbani1.at.live.com;

public class Stopwatch {
private long startTime = 0;
private boolean running = false;
private long currentTime = 0;

public void start() {
    this.startTime = System.currentTimeMillis();
    this.running = true;
}

public void stop() {
    this.running = false;
}

public void pause() {
    this.running = false;
    currentTime = System.currentTimeMillis() - startTime;
}
public void resume() {
    this.running = true;
    this.startTime = System.currentTimeMillis() - currentTime;
}

//elaspsed time in milliseconds
public long getElapsedTimeMili() {
    long elapsed = 0;
    if (running) {
         elapsed =((System.currentTimeMillis() - startTime)/100) % 1000 ;
    }
    return elapsed;
}

//elaspsed time in seconds
public long getElapsedTimeSecs() {
    long elapsed = 0;
    if (running) {
        elapsed = ((System.currentTimeMillis() - startTime) / 1000) % 60;
    }
    return elapsed;
}

  //elaspsed time in minutes
public long getElapsedTimeMin() {
    long elapsed = 0;
    if (running) {
        elapsed = (((System.currentTimeMillis() - startTime) / 1000) / 60 ) % 60;
    }
    return elapsed;
}

//elaspsed time in hours
public long getElapsedTimeHour() {
    long elapsed = 0;
    if (running) {
        elapsed = ((((System.currentTimeMillis() - startTime) / 1000) / 60 ) / 60);
    }
    return elapsed;
}

public String toString() {
    return getElapsedTimeHour() + ":" + getElapsedTimeMin() + ":"
            + getElapsedTimeSecs() + "." + getElapsedTimeMili();
}
}

问候

推荐阅读
Chloemw
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有