现在的位置: 首页 > 综合 > 正文

Android widget——Chronometer学习笔记

2013年09月12日 ⁄ 综合 ⁄ 共 6412字 ⁄ 字号 评论关闭

 Chronometer:

       Class that implements a simple timer.

       You can give it a start time in the elapsedRealtime() timebase, and it counts up from that, or if you don't give it a base time, it will use the time at which you call start(). By default it will display the current timer value in the form "MM:SS" or "H:MM:SS", or you can usesetFormat(String) to format the timer value into an arbitrary string.

       即,它是一个简单的计算器。

 

方法简介:

   long getBase(); //返回基地的时间,由setBase(long)设置的,可以是SystemClock.elapsedRealtime()  

   String getFormat();//返回当前字符串格式,此格式是通过setFormat()实现的
   void setBase(long base);//设置时间,计数定时器指定的值
   void setFormat(String format);//设置显示的内容,计时器将会显示这个参数所对应的值得,如果字符串的值
                                                为null,那么返回的值为MM:SS格式的

 

示例:

  chronometer.xml

 

ChronometerDemo.java

 

注意:如果你启动这个程序后,等上几秒再点击start,显示的时间不是从0开始的,而是从启动这个程序多长时后才开始计时的。点击stop后,时间仍然在继续加,只是显示的部分不变,再次点击start,时间会加上中间stop的时间。只有当点击reset后,才将base设置为当前的时间。

 

a question:

 http://stackoverflow.com/questions/526524/android-get-time-of-chronometer-widget

Android - Get time of chronometer widget

have a Chronometer widget in my Android app. I was wondering how to get the time from it. I tried getText, getFormat, getBase, etc, but none of them work. This is probably a easy question, but I could not find it on Google.
Thanks, Isaac Waller

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

If you look at the source of the Chronometer class, you'll see that it doesn't store the elapsed time in a field and it calculates it internally every time it needs to update the display.

However it's relatively easy to do the same in your own code:

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

This assumes that you have started your clock something like this:

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

Here's a full example:

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

One somewhat confusing thing about Chronometer is that you can't really use it as a stopwatch that gets started, stopped and restarted again. When it's running, it will always show the time elapsed since you last reset it, no matter how many times and for how long you have stopped it in the meantime. When it is stopped, it simply stops updating the display.

If you need something like a stopwatch you'll have to subclass Chronometer or maybe create your own version using the source.

alt text

 

抱歉!评论已关闭.