0

我有一个秒表,它工作正常,但是当我按下主页按钮或按钮关闭铃并在一段时间后重新打开它时,我在秒表中看到错误的时间。请告诉我我能做些什么来解决这个问题?

public class Stopwatch extends Activity {
    Timer mTimer;
    int second_f_p = 0, second_s_p = 0, millisecond = 0;
    int minute_f_p = 0, minute_s_p;
    int hour_f_p = 0, hour_s_p = 0;
    int numberOfLap = 0;
    TextView tv;
    TextView TVlap;
    Button btn_start;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stopwatch);

        btn_start = (Button) this.findViewById(R.id.button1);
        tv = (TextView) this.findViewById(R.id.tvTime);
        btn_start.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View arg0) {
                btn_start.setClickable(false);
                mTimer = new Timer();
                mTimer.schedule(new TimerTask() {

                    @Override
                    public void run() {
                        timerMethod();
                    }

                }, 0, 100);

            }
        });

        Button btn_stop = (Button) this.findViewById(R.id.button2);
        btn_stop.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {
                btn_start.setClickable(true);
                mTimer.cancel();
            }

        });

        Button btn_reset = (Button) this.findViewById(R.id.button4);
        btn_reset.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {

                second_f_p = 0;
                second_s_p = 0;
                minute_f_p = 0;
                minute_s_p = 0;
                millisecond = 0;
                hour_f_p = 0;
                hour_s_p = 0;
                numberOfLap = 0;
                TVlap.setText("");
                tv.setText(String.valueOf(hour_f_p) + String.valueOf(hour_s_p) + ":" + String.valueOf(minute_f_p) + String.valueOf(minute_s_p) + ":" + String.valueOf(second_f_p) + String.valueOf(second_s_p) + "." + String.valueOf(millisecond));
            }

        });
        TVlap = (TextView) this.findViewById(R.id.tvLap);
        Button btn_lap = (Button) this.findViewById(R.id.button3);
        btn_lap.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {
                numberOfLap++;
                TVlap.setText(TVlap.getText() + String.valueOf(numberOfLap) + ". " + String.valueOf(hour_f_p) + String.valueOf(hour_s_p) + ":" + String.valueOf(minute_f_p) + String.valueOf(minute_s_p) + ":" + String.valueOf(second_f_p) + String.valueOf(second_s_p) + "." + String.valueOf(millisecond) + "\n");
            }

        });

    }

    private void timerMethod() {
        this.runOnUiThread(doSomething);
    }

    private final Runnable doSomething = new Runnable() {@Override
        public void run() {
            millisecond++;

            if (millisecond == 10) {
                second_s_p++;
                millisecond = 0;

            }

            if (second_s_p == 10) {
                second_f_p++;
                second_s_p = 0;
            }
            if (second_f_p == 6) {
                minute_s_p++;
                second_f_p = 0;
            }
            if (minute_s_p == 10) {
                minute_f_p++;
                minute_s_p = 0;
            }
            if (minute_f_p == 6) {
                hour_s_p++;
                minute_f_p = 0;
            }

            if (hour_s_p == 10) {
                hour_f_p++;
                hour_s_p = 0;
            }

            if (hour_f_p == 9 && hour_s_p == 9) {
                second_f_p = 0;
                second_s_p = 0;
                minute_f_p = 0;
                minute_s_p = 0;
                hour_f_p = 0;
                hour_s_p = 0;
            }
            tv.setText(String.valueOf(hour_f_p) + String.valueOf(hour_s_p) + ":" + String.valueOf(minute_f_p) + String.valueOf(minute_s_p) + ":" + String.valueOf(second_f_p) + String.valueOf(second_s_p) + "." + String.valueOf(millisecond));
        }

    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_stopwatch, menu);
        return true;
    }

}
4

1 回答 1

0

请参阅下面的代码链接

http://www.edumobile.org/android/android-development/stop-watch-example/

如果您查看 Chronometer 类的源代码,您会发现它不会将经过的时间存储在字段中,并且每次需要更新显示时都会在内部计算它。

然而,在你自己的代码中做同样的事情相对容易:

long elapsedMillis = SystemClock.elapsedRealtime() - chronometerInstance.getBase();

这假设你已经开始你的时钟是这样的:

chronometerInstance.setBase(SystemClock.elapsedRealtime());
chronometerInstance.start();

这是一个完整的例子:

public class ChronoExample extends Activity {
Chronometer mChronometer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);

    mChronometer = new Chronometer(this);
    layout.addView(mChronometer);

    Button startButton = new Button(this);
    startButton.setText("Start");
    startButton.setOnClickListener(mStartListener);
    layout.addView(startButton);

    Button stopButton = new Button(this);
    stopButton.setText("Stop");
    stopButton.setOnClickListener(mStopListener);
    layout.addView(stopButton);

    Button resetButton = new Button(this);
    resetButton.setText("Reset");
    resetButton.setOnClickListener(mResetListener);
    layout.addView(resetButton);        

    setContentView(layout);
}

private void showElapsedTime() {
    long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase();            
    Toast.makeText(ChronoExample.this, "Elapsed milliseconds: " + elapsedMillis, 
            Toast.LENGTH_SHORT).show();
}

View.OnClickListener mStartListener = new OnClickListener() {
    public void onClick(View v) {
        mChronometer.start();
        showElapsedTime();
    }
};

View.OnClickListener mStopListener = new OnClickListener() {
    public void onClick(View v) {
        mChronometer.stop();
        showElapsedTime();
    }
};

View.OnClickListener mResetListener = new OnClickListener() {
    public void onClick(View v) {
        mChronometer.setBase(SystemClock.elapsedRealtime());
        showElapsedTime();
    }
};
}

关于 Chronometer 的一个有点令人困惑的事情是,您不能真正将它用作开始、停止和重新启动的秒表。当它运行时,它将始终显示自上次重置以来经过的时间,无论您在此期间停止了多少次和多长时间。当它停止时,它只是停止更新显示。

于 2012-11-26T07:36:17.810 回答