欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

學(xué)習(xí)使用Android Chronometer計(jì)時(shí)器

 更新時(shí)間:2021年04月14日 11:48:38   作者:amigos_pop  
Chronometer是一個(gè)簡(jiǎn)單的計(jì)時(shí)器,你可以給它一個(gè)開始時(shí)間,并以計(jì)時(shí),或者如果你不給它一個(gè)開始時(shí)間,它將會(huì)使用你的時(shí)間通話開始,這篇文章主要幫助大家學(xué)習(xí)掌握使用Android Chronometer計(jì)時(shí)器,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android Chronometer計(jì)時(shí)器基本使用方法,供大家參考,具體內(nèi)容如下

在默認(rèn)情況下,Chronometer組件只輸出MM:SS或H:MM:SS的時(shí)間格式。例如,當(dāng)計(jì)時(shí)到1分20秒時(shí),Chronometer組件會(huì)顯示01:20。如果想改變顯示的信息內(nèi)容,可以使用Chronometer類的setFormat方法。該方法需要一個(gè)String變量,并使用"%s"表示計(jì)時(shí)信息。例如,使用setFormat("計(jì)時(shí)信息:%s")設(shè)置顯示信息,Chronometer組件會(huì)顯示如下計(jì)時(shí)信息:

計(jì)時(shí)信息:10:20

android:format;//定義時(shí)間的格式如:hh:mm:ss
 setFormat("計(jì)時(shí):%s"); //設(shè)置顯示格式
 setFormat(String format);//設(shè)置顯示時(shí)間的格式。 
start();//開始計(jì)時(shí) 
stop();//停止計(jì)時(shí)

setBase();//設(shè)置基地時(shí)間,一般都是SystemClock.elapsedRealtime() 
setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener);//當(dāng)計(jì)時(shí)器改變時(shí)調(diào)用

案例:

1.定義布局文件chronometer.xml

<?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/chronometer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   />
  <Button
    android:id="@+id/chronometer_start"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="開始計(jì)時(shí)"
  />
  <Button
    android:id="@+id/chronometer_end"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="停止計(jì)時(shí)"
     
  />
  <Button
    android:id="@+id/chronometer_null"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="重新開始計(jì)時(shí)"
     
  />
</LinearLayout>

2.java代碼文件:ChronometerDemo.java

package com.test;
 
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 ChronometerDemo extends Activity {
   
  private Chronometer chronometer;
   
  private Button button_start, button_end,button_bull;
   
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
     
    setContentView(R.layout.chronometer);
     
    chronometer =(Chronometer)findViewById(R.id.chronometer);
    button_start =(Button)findViewById(R.id.chronometer_start);
    button_end =(Button)findViewById(R.id.chronometer_end);
    button_bull =(Button)findViewById(R.id.chronometer_null);
     
    button_start.setOnClickListener(clickListener);
    button_end.setOnClickListener(clickListener);
    button_bull.setOnClickListener(clickListener);
  }
 
  private OnClickListener clickListener = new OnClickListener() {
     
    @Override
    public void onClick(View v) {
       
      switch (v.getId()) {
      case R.id.chronometer_start:
        //調(diào)用start()方法開始計(jì)時(shí)
        chronometer.start();
        button_start.setText("正在計(jì)時(shí)...");
        break;
      case R.id.chronometer_end:
        //調(diào)用stop()方法停止計(jì)時(shí)
        chronometer.stop();
        button_start.setText("繼續(xù)計(jì)時(shí)");
        break;
      case R.id.chronometer_null:
        //調(diào)用stop()方法停止計(jì)時(shí)
        chronometer.setBase(SystemClock.elapsedRealtime());
        chronometer.start();
        button_start.setText("正在計(jì)時(shí)...");
        break;
 
      default:
        break;
      }
    }
  };
}

3. 運(yùn)行效果:

有一個(gè)問題就是,計(jì)時(shí)器開始計(jì)時(shí)后,一段時(shí)間之后,點(diǎn)擊停止計(jì)時(shí),一段時(shí)間后,點(diǎn)擊繼續(xù)計(jì)時(shí),但是這個(gè)時(shí)間的不是停止后的那個(gè)時(shí)間。說明停止之后,計(jì)時(shí)器還在后臺(tái)運(yùn)行。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)Android Chronometer計(jì)時(shí)器有所幫助和啟發(fā)。

相關(guān)文章

最新評(píng)論