Chronometer android計時器組件Chronometer的使用,android通話時間長度計時控制項

來源:互聯網
上載者:User

標籤:android   android計時器   控制項   chronometer的使用   安卓中的chronometer   

在安卓開發過程中經常需要計數器的使用,我們可以用handler實現間隔性的操作。

例如按兩次返回back退出,隔幾秒重複換頁面等。

但是也經常有一類需求類是打電話時候記錄時間長度的操作。

這就用到了android中 android.widget包下的Chronometer 這個組件。


轉載請註明:謝謝


Chronometer介紹

Class that implements a simple timer.
 * <p>
 * You can give it a start time in the {@link SystemClock#elapsedRealtime} timebase,
 * and it counts up from that, or if you don‘t give it a base time, it will use the
 * time at which you call {@link #start}.  By default it will display the current
 * timer value in the form "MM:SS" or "H:MM:SS", or you can use {@link #setFormat}
 * to format the timer value into an arbitrary string.
 *
 * @attr ref android.R.styleable#Chronometer_format



本人英語不好 ,就不在這獻醜了。抱歉。。。。。



首先先看布局檔案



三個按鈕和 一個Chronometer的控制項 沒有特殊方法



Activity邏輯代碼部分


大部分方法都已經注釋

注意 chronometer.getText();方法返回的CharSequence這個對象,我們通過這個對象可以操作裡面的資料

CharSequence類裡面的方法有:

 public int length();

 public char charAt(int index);

 public CharSequence subSequence(int start, int end);

 public String toString();

這四個方法看名字也知道啥意思第一個擷取長度、第二根據位置擷取字元、第三個截取、第四個toString


我們可以根據擷取到的屬性進行不同的操作,例如:到5秒的時候進行操作可以

if("00:05".equals(text.toString())){

  要進行的操作;

}


也可以擷取字元

if( text.charAt(text.length()-1)==5){

  要進行的操作;

}


還可以自己拆分字串進行操作總之各種方法就不獻醜了。



這就是一個簡單計時器在android開發中的使用。




Chronometer android計時器組件Chronometer的使用,android通話時間長度計時控制項

聯繫我們

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