秒表逻辑秒表、逻辑

2023-09-12 23:38:22 作者:指尖旋律╮演奏了谁的悲伤

我要开发Android的一个简单的秒表的逻辑。

I want to develop a simple stop watch logic in android.

在单击列表视图中的计时器应启动并单击按钮时定时器应停止。任何人都可以请指导我。任何样品code将有很大的帮助。

On clicking a list view the timer should start and on clicking the button the timer should stop. Can anyone please guide me. Any sample code will be of great help

推荐答案

使用秒表类(高于precision使用 System.nanoTime()

Use the Stopwatch Class (For higher precision use System.nanoTime())

添加到启动()事件和Stop()事件上的按钮presses。您需要更新UI所以使用一个线程/处理器组合。

Add a Start() event and Stop() event on Button Presses. You'll need to update the UI so use a Thread/Handler Combination.

这应该让你开始。

编辑:添加code。 (尼斯锻炼!:))

Added Code. (Nice Exercise! :) )

使用了 Refresh_Rate 配置多久你的UI进行更新。

Use the Refresh_Rate to configure how often your UI is updated.

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);
        }
    }
}