Android - Get time of chronometer widget

2019-01-03 23:35发布

How to get the time from a Chronometer? I tried getText, getFormat, getBase, etc, but none of them could work.

Example code snippet:

Chronometer t = (Chronometer)findViewById(R.id.toptime);
long time = SystemClock.elapsedRealtime()-t.getBase();
Log.d(null,"Was: "+time); //time is not the proper time for some reason - it is a random number between 0 and 50
t.setBase(SystemClock.elapsedRealtime());
t.start();

7条回答
Evening l夕情丶
2楼-- · 2019-01-04 00:38
//ok here is the final changed code which works well

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.LinearLayout;
import android.widget.Toast;

public class HelloWidgetActivity 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(HelloWidgetActivity.this,
                "Elapsed milliseconds: " + elapsedMillis, Toast.LENGTH_SHORT).show();
    }



    View.OnClickListener mStartListener = new OnClickListener() {
        public void onClick(View v) {
          int stoppedMilliseconds = 0;
          String chronoText = mChronometer.getText().toString();
          String array[] = chronoText.split(":");
          if (array.length == 2) {
            stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
                + Integer.parseInt(array[1]) * 1000;
          } else if (array.length == 3) {
            stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000 
                + Integer.parseInt(array[1]) * 60 * 1000
                + Integer.parseInt(array[2]) * 1000;
          }
          mChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
          mChronometer.start();
        }
      };

    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());
            mChronometer.stop();
            showElapsedTime();
        }
    };
}
查看更多
登录 后发表回答