Android Hands-on tutorial 40th chronometer implementation Countdown _android

Source: Internet
Author: User

Android provides an API for timing by seconds, and today is a simple countdown to this API.

To make a layout:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical " > <chronometer android:id= "@+id/mychronometer" android:layout_width= "Wrap_content" Wrap_content "/> <linearlayout android:layout_width=" wrap_content "android:layout_height=" an 
  droid:orientation= "Horizontal" > <button android:id= "@+id/btnstart" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Start Timer"/> <button android:id= "@+id/btnstop" Android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Stop"/> <Button  
 
 D= "@+id/btnbase" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "reset"/> <button android:id= "@+id/bTnformat "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text=" format "/>" 
 ;/linearlayout> </LinearLayout>

The code in the

corresponding activity is as follows:

Package Com.example.timer; 
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; 
 public class Mainactivity extends activity {private chronometer mychronometer = null; 
 Private Button btnstart = null; 
 Private Button btnstop = null; 
 Private Button btnbase = null; 
 
 Private Button Btnformat = null; public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//life cycle method Super.setcontentview (R . Layout.activity_main); 
 Set the layout manager to use This.mychronometer = (chronometer) super. Findviewbyid (R.id.mychronometer); 
 btnstart = (Button) Super.findviewbyid (R.id.btnstart); 
 Btnstop = (Button) Super.findviewbyid (r.id.btnstop); 
 Btnbase = (Button) Super.findviewbyid (r.id.btnbase); 
 Btnformat = (Button) Super.findviewbyid (R.id.btnformat); Btnstart.setonclicklistener (New OnclicklistEnerstart ()); 
 Btnstop.setonclicklistener (New Onclicklistenerstop ()); 
 Btnbase.setonclicklistener (New Onclicklistenerbase ()); 
 Btnformat.setonclicklistener (New Onclicklistenerformat ()); Private class Onclicklistenerstart implements Onclicklistener {public void OnClick (View arg0) {Mychronomete 
 
 R.start (); } Private class Onclicklistenerstop implements Onclicklistener {public void OnClick (View arg0) {MyChron 
 
 Ometer.stop (); } Private class Onclicklistenerbase implements Onclicklistener {public void OnClick (View arg0) {MyChron 
 
 Ometer.setbase (Systemclock.elapsedrealtime ()); } Private class Onclicklistenerformat implements Onclicklistener {public void OnClick (View arg0) {MYCHR Onometer.setformat ("New display format:%s"). 
 
 "); 
 } 
 
 } 
 
}

Running run up look at the effect:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.