Android Learning notes-timer implementation

Source: Internet
Author: User

Write code based on Android crazy handouts and summarize the written code in your blog. The function is simple: A simple timer, click the Start button will start the timer, when the time to 20 seconds will automatically stop the timing.

The interface is as follows:

Interface code:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?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"         >     <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="启动"             /> </LinearLayout>

Activity.java

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 package com.example.test3_3_12;import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Chronometer; import android.os.SystemClock;public class MyActivity extends Activity {     Button start;     Chronometer ch;     /**      * Called when the activity is first created.      */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         ch=(Chronometer)findViewById(R.id.test);         start=(Button) findViewById(R.id.start);         start.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 //设置开始时间                 ch.setBase(SystemClock.elapsedRealtime());                 //启动计时器                 ch.start();                 start.setEnabled(false);             }         });         //为Chronometer绑定事件监听器         ch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {             @Override             public void onChronometerTick(Chronometer chronometer) {                 //如果从开始计时到现在超过了20s                 if(SystemClock.elapsedRealtime()-ch.getBase()>20*1000)                 {                     ch.stop();                     start.setEnabled(true);                }             }         });    } }

One of the timer components used here is mainly: chronometer, which is inherited TextView, so it can display a paragraph, but this text can only display the time, and is not the current time, but a time period (difference).

Chronometer supports a common approach:

SetBase (long Base): Sets the start time of the timer. SetBase (Systemclock.elapsedrealtime ()) Set the start time to the current time of the system, Systemclock need to import Android.os.SystemClock header file.

SetFormat (String format): Sets the format of the display time.

Start (): Start timing.

Stop (): Stop timing.

Setonchronometerticklistener (Chronometer.onchronometerticklistener Listener) is the timer's event listener when a timer change is triggered by that listener.

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.