Preface
This chapter is about android. widget. Chronometer. The version is Android 2.2 r1. I look forward to your participation in Android API Chinese translation, contact me over140@gmail.com.
Statement
You are welcome to repost, but please keep the original source of the article :)
Blog Garden: http://www.cnblogs.com/
Android Chinese translation group: http://www.cnblogs.com/over140/
Body
I. Structure
Public class Chronometer extends TextView
Java. lang. Object
Android. view. View
Android. widget. TextView
Android. widget. Chronometer
Ii. Overview
Class implements a simple timer.
You can useelapsedRealtime()ComeGive it a benchmark time and count it from that time. If you do not give it a benchmark time, it will use your callstart()Time. By default, it will display the current time in the format of "MM: SS" or "H: MM: SS", or you cansetFormat(String)Set an arbitrary string to format the display time of the timer.
Iii. XML attributes
Attribute name |
Description |
Android: format |
Formatted string: if specified, the timer is displayed based on this string. Replace the first "% s" in the string with "MM: SS" or "H: MM: SS "format time display. If this parameter is not specified, the timer will display the time in the format of "MM: SS" or "H: MM: SS. Note: For example:“This is a Chronometer %s”) |
Iv. Constructor
Public Chronometer (Context context)
Initialize the timer object. Set the current time as the reference time. (Translator's note: Using programs to dynamically create timer objects)
Public Chronometer (Context context, AttributeSet attrs)
Initialize standard view layout information. Set the current time as the reference time. (Note: specify a timer using XML)
Public Chronometer (Context context, AttributeSet attrs, int defStyle)
Initialize standard view layout information and style. Set the current time as the reference time.
V. Public Methods
Public long getBase ()
Returns the benchmark time previously set by setBase (long.
Public String getFormat ()
Returns the formatted String previously set by setFormat (String.
Public Chronometer. OnChronometerTickListener getOnChronometerTickListener ()
Return Value
The listener (which may be null) is used to listen for timer changes.
Public void setBase (long base)
Set the benchmark time, instead of the start time, for example, calling this function and setting the base parameter as SystemClock. elapsedRealtime () indicates to re-timing from the current time ).
Parameters
Base uses elapsedRealtime () as the benchmark time
Public void setFormat (String format)
Set the format string to be displayed. Formatted string: if specified, the timer is displayed based on this string. Replace the first "% s" in the string with "MM: SS" or "H: MM: SS "format time display. If the format string is empty or you have never called the setFormat () method, the timer will simply display the time in the format of "MM: SS" or "H: MM: SS. Note: For example:"This is a Chronometer %s")
Parameters
Format formatted string
Public void setOnChronometerTickListener (Chronometer. OnChronometerTickListener listener)
Sets the listener events that are called when the timer changes.
Parameters
Listener The listener.
Public void start ()
Start timing. It does not affect the benchmark time set by setBase (long). Only the view is displayed. Even if the widget is not displayed, the timer periodically processes messages. To avoid resource leakage, you must ensure that each start () method has a corresponding stop () call ). (Note: The start command only displays the timing. In fact, the timing starts from the benchmark time. Therefore, when the timer is stopped for several seconds and then started, the displayed timer will jump to several seconds after the currently displayed timer and continue the timer. See this post .)
Public void stop ()
Stop timing. It does not affect the benchmark time set by setBase (long). Only the view is displayed. This will stop sending messages and effectively release the resources occupied by start () when the timer is running.
Vi. Protected Methods
Protected void onDetachedFromWindow ()
A view is called when it is removed from the form, and the view is no longer displayed on the form surface.
Protected void onWindowVisibilityChanged (int visibility)
This API is called when the visibility (GONE, INVISIBLE, and VISIBLE) of a view is changed in a form. Note that it will tell you whether your window can be recognized by the window manager. This does not indicate whether the window is blocked by other windows on the screen, even if it is visible.
Parameters
New visibility of the visibility window
VII. Supplement
Article Link
Time Service in android-Chronometer timer Service
Sample Code
Java files
Public class ChronometerDemo extends Activity {
Private Chronometer cher1;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. chronometer );
Cher1 = (Chronometer) findViewById (R. id. cher1 );
Cher1.setFormat ("Timing: % s ");
}
/**
* Start timing
* @ Param view
*/
Public void onStart (View view ){
Cher1.start ();
}
/**
* Stop timing
* @ Param view
*/
Public void onStop (View view ){
Cher1.stop ();
}
/**
* Reset
* @ Param view
*/
Public void onReset (View view ){
Cher1.setBase (SystemClock. elapsedRealtime ());
}
}
XML file
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content">
<Chronometer android: id = "@ + id/cher1" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"> </Chronometer>
<LinearLayout android: layout_width = "wrap_content" android: layout_height = "wrap_content">
<Button android: onClick = "onStart" android: text = "start timing" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button>
<Button android: onClick = "onStop" android: text = "Stop timing" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button>
<Button android: onClick = "onReset" android: text = "reset" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button>
</LinearLayout>
</LinearLayout>
End
I translated this article half a month ago, and I have been working on the school draft and publishing :)