Timer Program, hitting start, timer starts timing, stops automatically after 20 seconds, uses the timer control
Package org. crazyit. time;
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. Chronometer. OnChronometerTickListener;
Public class ChronometerTest extends Activity
{
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Obtain the timer component
Final Chronometer ch = (Chronometer) findViewById (R. id. test );
// Obtain the start button
Button start = (Button) findViewById (R. id. start );
Start. setOnClickListener (new OnClickListener ()
{
@ Override
Public void onClick (View source)
{
// Set the start time
Ch. setBase (SystemClock. elapsedRealtime ());
// Start the timer
Ch. start ();
}
});
// Timer listener
Ch. setOnChronometerTickListener (new OnChronometerTickListener ()
{
@ Override
Public void onChronometerTick (Chronometer ch)
{
// If the time has exceeded 20 s since the start of the timer.
If (SystemClock. elapsedRealtime ()-ch. getBase ()
> 20*1000)
{
Ch. stop ();
}
}
});
}
}
Package org. crazyit. time;
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. Chronometer. OnChronometerTickListener;
Public class ChronometerTest extends Activity
{
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Obtain the timer component
Final Chronometer ch = (Chronometer) findViewById (R. id. test );
// Obtain the start button
Button start = (Button) findViewById (R. id. start );
Start. setOnClickListener (new OnClickListener ()
{
@ Override
Public void onClick (View source)
{
// Set the start time
Ch. setBase (SystemClock. elapsedRealtime ());
// Start the timer
Ch. start ();
}
});
// Timer listener
Ch. setOnChronometerTickListener (new OnChronometerTickListener ()
{
@ Override
Public void onChronometerTick (Chronometer ch)
{
// If the time has exceeded 20 s since the start of the timer.
If (SystemClock. elapsedRealtime ()-ch. getBase ()
> 20*1000)
{
Ch. stop ();
}
}
});
}
}
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: gravity = "center_horizontal"
>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: gravity = "center_horizontal"
>
<! -- Timer control -->
<Chronometer
Android: id = "@ + id/test"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textSize = "12pt"
Android: textColor = "# ffff0000"
/>
<Button
Android: id = "@ + id/start"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "start"
/>
</LinearLayout>
From the column hn307165411