Android – 获取计时器小部件的时间

如何从天文台得到时间? 我尝试了getText,getFormat,getBase等,但没有一个可以工作。

示例代码片段:

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

如果您查看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(); } }; } 

关于天文台一个有点令人困惑的事情是,你不能真正使用它作为一个秒表,开始,停止和重新启动。 当它运行时,它会一直显示自上次重置之后所经过的时间, 无论次数多less以及停止多长时间 。 停止时,只停止更新显示。

如果你需要像秒表这样的东西,你必须inheritance天文台时间表,或者使用源代码创build你自己的版本。

替代文字

我发现这个例子非常有用,谢谢nyenyec!

这是我的两分钱如何把它变成一个真正的秒表function,没有inheritance天文台。 只需要改变mStartListener方法来parsingmChronometer中的文本(毕竟它是从TextView派生的),计算毫秒数,然后使用setBase()将基准时间重新调整为过去的时间量:

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

@nyenyec + 1:这是我最终的结果,而使用nyenyec的回应没有一个子类。

  chronometer.setOnChronometerTickListener(new OnChronometerTickListener() { @Override public void onChronometerTick(Chronometer chronometer) { long elapsedMillis = SystemClock.elapsedRealtime() - chronometer.getBase(); if(elapsedMillis>THRESHOLD){ doYourStuff(); } } }); 

THRESHOLD在哪里

 private static final int THRESHOLD_EXERSISE = 60000; //In milliseconds 

我的解决scheme

  public void starttimer(View view){ Button mybtn = (Button) view; if (mybtn.equals(findViewById(R.id.button1))) { mycm.setBase(SystemClock.elapsedRealtime() - elapsed); mycm.start(); } else { mycm.stop(); elapsed = SystemClock.elapsedRealtime() - mycm.getBase(); } } 

在onCreate:

  mycm = (Chronometer) findViewById(R.id.chronometer1); elapsed = 0; 

布局中有两个button,它们都调用starttimer方法(启动和停止)

有点迟到的反应,但我今天试图自己解决这个问题。 我结束了parsing视图的文本:

  // Expects a string in the form MM:SS or HH:MM:SS public static int getSecondsFromDurationString(String value){ String [] parts = value.split(":"); // Wrong format, no value for you. if(parts.length < 2 || parts.length > 3) return 0; int seconds = 0, minutes = 0, hours = 0; if(parts.length == 2){ seconds = Integer.parseInt(parts[1]); minutes = Integer.parseInt(parts[0]); } else if(parts.length == 3){ seconds = Integer.parseInt(parts[2]); minutes = Integer.parseInt(parts[1]); hours = Integer.parseInt(parts[0]); } return seconds + (minutes*60) + (hours*3600); } 

因此,使用view.getText()。toString()调用getSecondsFromDurationString会以秒为单位给您总的运行时间(我的应用程序是某种秒表,因此您可以暂停并继续运行)。

希望它有帮助。

  final Chronometer counter = (Chronometer) findViewById(R.id.chronometer1); counter.setOnChronometerTickListener(new OnChronometerTickListener() { public void onChronometerTick(Chronometer chronometer) { // TODO Auto-generated method stub chronometer.refreshDrawableState(); } }); final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.togglebutton1); togglebutton.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Perform action on clicks if (togglebutton.isChecked()) { counter.start(); } else { counter.stop(); } } }); 
 //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(); } }; }