Android控制項開發之Chronometer

來源:互聯網
上載者:User

Chronometr是一個簡單的定時器,你可以給它一個開始時間,並以此定時,或者如果你不給它一個開始時間,

它將會使用你的時間通話開始。預設情況下它會顯示在當前定時器的值得形式"分:秒" 或"MM:SS",

 或者可以使用set(字串)的定時器值到一個任一字元串


重要屬性

android:format    定義時間的格式如: hh:mm:ss

重要方法

setBase(long base)//設定倒計時定時器setFormat(String format)//設定顯示時間的格式start()//開始計時stop()//停止計時                                //當計時器改變時調用setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener)




常式main.xml源碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"    android:layout_width="wrap_content"     android:layout_height="wrap_content"><Chronometer    android:id="@+id/chronometer"     android:layout_width="wrap_content"    android:layout_height="wrap_content"/><LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"><Button android:onClick="onStart" android:text="開始計時" android:layout_width="wrap_content" android:layout_height="wrap_content"/><Button android:onClick="onStop" android:text="停止計時" android:layout_width="wrap_content" android:layout_height="wrap_content"/><Button android:onClick="onReset" android:text="重設" android:layout_width="wrap_content" android:layout_height="wrap_content"/>   </LinearLayout></LinearLayout>

Java源碼

ackage com.sx.Chronometer;import android.app.Activity;import android.os.Bundle;import android.os.SystemClock;import android.view.View;import android.widget.Chronometer;public class ChronometerActivity extends Activity {private Chronometer chronometer = null;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){    super.onCreate(savedInstanceState);    setContentView(R.layout.main);        chronometer = (Chronometer) findViewById(R.id.chronometer);    chronometer.setFormat("計時:%s");}public void onStart(View view) { chronometer.start();}public void onStop(View view) { chronometer.stop();}public void onReset(View view){ chronometer.setBase(SystemClock.elapsedRealtime());}}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.