国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實(shí)戰(zhàn)教程第四十篇之Chronometer實(shí)現(xiàn)倒計時

2019-12-12 04:43:52
字體:
供稿:網(wǎng)友

Android提供了實(shí)現(xiàn)按照秒計時的API,今天就是用這個API實(shí)現(xiàn)簡單的倒計時。

來個布局:

<?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="復(fù)位" />   <Button   android:id="@+id/btnFormat"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="格式化" />  </LinearLayout>  </LinearLayout> 

對應(yīng)活動中的代碼如下:

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); // 設(shè)置要使用的布局管理器  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。");   }   }  } 

運(yùn)行跑起來看看效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 凤山市| 枝江市| 临颍县| 汉中市| 当雄县| 丰镇市| 锦州市| 连平县| 白朗县| 民权县| 凤冈县| 稻城县| 五大连池市| 长汀县| 满洲里市| 建瓯市| 重庆市| 县级市| 汕头市| 兴业县| 莱州市| 望都县| 高要市| 临汾市| 土默特右旗| 汨罗市| 唐山市| 金山区| 泾阳县| 长顺县| 和静县| 旌德县| 牡丹江市| 陇西县| 威海市| 呈贡县| 侯马市| 宁晋县| 芜湖市| 长白| 绥德县|