計(jì)數(shù)器通常用于Android開發(fā)。我們可以使用處理程序來實(shí)現(xiàn)間隔操作,下面就跟著武林技術(shù)頻道小編的步伐詳解Android自帶倒計(jì)時(shí)控件Chronometer使用方法吧。
先看看效果:

<Chronometer android:id="@+id/chronometer" android:layout_width="wrap_content" android:layout_height="30dp" /> <Button android:onClick="start" android:text="開始" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:onClick="stop" android:text="停止" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:onClick="reset" android:text="重置" android:layout_width="wrap_content" android:layout_height="wrap_content" />
方法簡介:

long getBase(); //返回基地的時(shí)間,由setBase(long)設(shè)置的,可以是SystemClock.elapsedRealtime() String getFormat();//返回當(dāng)前字符串格式,此格式是通過setFormat()實(shí)現(xiàn)的void setBase(long base);//設(shè)置時(shí)間,計(jì)數(shù)定時(shí)器指定的值void setFormat(String format);//設(shè)置顯示的內(nèi)容,計(jì)時(shí)器將會顯示這個(gè)參數(shù)所對應(yīng)的值得,如果字符串的值為null,那么返回的值為MM:SS格式的
private Chronometer chronometer; private long recordTime;//記錄下來的總時(shí)間 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); chronometer= (Chronometer) findViewById(R.id.chronometer); chronometer.setFormat("計(jì)時(shí):%s");//設(shè)置顯示的格式 Toast.makeText(MainActivity.this, ""+recordTime, Toast.LENGTH_SHORT).show(); } public void start(View view){ chronometer.setBase(SystemClock.elapsedRealtime()-recordTime);//跳過已經(jīng)記錄的時(shí)間 chronometer.start(); } public void stop(View view){ chronometer.stop(); recordTime=SystemClock.elapsedRealtime()-chronometer.getBase();//保存這次記錄的時(shí)間 } public void reset(View view){ recordTime=0;//重置時(shí)間 chronometer.setBase(SystemClock.elapsedRealtime()); } 以上就是武林技術(shù)頻道小編為大家?guī)淼脑斀釧ndroid自帶倒計(jì)時(shí)控件Chronometer使用方法,看了以上文章,大家對倒計(jì)時(shí)控件有沒有更加了解了呢?希望大家可以學(xué)習(xí)帶有用的知識。
新聞熱點(diǎn)
疑難解答
圖片精選