Android study note 13: Timer

Source: Internet
Author: User
Tags getbase

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.