Android實戰教程第四十篇之Chronometer實現倒計時_Android

來源:互聯網
上載者:User

Android提供了實現按照秒計時的API,今天就是用這個API實現簡單的倒計時。

來個布局:

<?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"  android:layout_height="wrap_content" />   <LinearLayout  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:orientation="horizontal" >   <Button   android:id="@+id/btnStart"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="開始計時" />   <Button   android:id="@+id/btnStop"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="停止" />   <Button   android:id="@+id/btnBase"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="複位" />   <Button   android:id="@+id/btnFormat"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="格式化" />  </LinearLayout>  </LinearLayout> 

對應活動中的代碼如下:

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); // 生命週期方法  super.setContentView(R.layout.activity_main); // 設定要使用的布局管理器  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) {   myChronometer.start();   }   }   private class OnClickListenerStop implements OnClickListener {   public void onClick(View arg0) {   myChronometer.stop();   }   }   private class OnClickListenerBase implements OnClickListener {   public void onClick(View arg0) {   myChronometer.setBase(SystemClock.elapsedRealtime());   }   }   private class OnClickListenerFormat implements OnClickListener {   public void onClick(View arg0) {   myChronometer.setFormat("新的顯示格式:%s。");   }   }  } 

運行跑起來看看效果:

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.