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

Android Chronometer控件實(shí)現(xiàn)計(jì)時(shí)器函數(shù)詳解

 更新時(shí)間:2016年04月20日 17:24:04   投稿:lijiao  
這篇文章主要為大家詳細(xì)介紹了Android Chronometer控件實(shí)現(xiàn)計(jì)時(shí)器函數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家演示了如何使用Chronometer控件實(shí)現(xiàn)Android計(jì)時(shí)器的實(shí)例。

先貼上最終的實(shí)現(xiàn)效果圖:

Android Chronometer控件實(shí)現(xiàn)計(jì)時(shí)器

Android計(jì)時(shí)器實(shí)現(xiàn)思路

使用Chronometer控件實(shí)現(xiàn)計(jì)器的操作。通過(guò)設(shè)置setBase(long base)來(lái)設(shè)置初始時(shí)間,然后為其添加一個(gè) setOnChronometerTickListener(Chronometer.OnChronometerTickListener l)事件來(lái)判斷時(shí)間是否到了,然后再調(diào)用其stop()方法實(shí)現(xiàn)停止計(jì)時(shí)。

Android計(jì)時(shí)器實(shí)現(xiàn)代碼
main.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:background="@drawable/back" 
  android:gravity="center" 
  android:orientation="vertical" > 
 
  <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dip" 
    android:orientation="horizontal" > 
 
    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="4" 
      android:gravity="center" 
      android:text="設(shè)置時(shí)間:" /> 
 
    <EditText 
      android:id="@+id/edt_settime" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:inputType="number" /> 
  </LinearLayout> 
 
  <Chronometer 
    android:id="@+id/chronometer" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:textColor="#ff0000" 
    android:textSize="60dip" /> 
 
  <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dip" 
    android:orientation="horizontal" > 
 
    <Button 
      android:id="@+id/btnStart" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="開(kāi)始記時(shí)" /> 
 
    <Button 
      android:id="@+id/btnStop" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="停止記時(shí)" /> 
 
    <Button 
      android:id="@+id/btnReset" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="重置" /> 
  </LinearLayout> 
 
</LinearLayout> 

Activity代碼:

package com.jiahui.chronometer;  
 
import android.app.Activity;  
import android.app.AlertDialog;  
import android.app.Dialog;  
import android.content.DialogInterface;  
import android.os.Bundle;  
import android.os.SystemClock;  
import android.text.format.Time;  
import android.view.View;  
import android.widget.Button;  
import android.widget.Chronometer;  
import android.widget.EditText;  
 
public class ChronometerDemoActivity extends Activity {  
 
  private int startTime = 0;  
 
  public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  
 
    final Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer);  
 
    Button btnStart = (Button) findViewById(R.id.btnStart);  
 
    Button btnStop = (Button) findViewById(R.id.btnStop);  
 
    Button btnRest = (Button) findViewById(R.id.btnReset);  
 
    final EditText edtSetTime = (EditText) findViewById(R.id.edt_settime);  
 
    btnStart.setOnClickListener(new View.OnClickListener() {  
 
      @Override 
      public void onClick(View v) {  
 
        System.out.println("--開(kāi)始記時(shí)---");  
        String ss = edtSetTime.getText().toString();  
        if (!(ss.equals("") && ss != null)) {  
          startTime = Integer.parseInt(edtSetTime.getText()  
              .toString());  
        }  
        // 設(shè)置開(kāi)始講時(shí)時(shí)間  
        chronometer.setBase(SystemClock.elapsedRealtime());  
        // 開(kāi)始記時(shí)  
        chronometer.start();  
 
      }  
    });  
 
    btnStop.setOnClickListener(new View.OnClickListener() {  
      @Override 
      public void onClick(View v) {  
        // 停止  
        chronometer.stop();  
      }  
 
    });  
 
    // 重置  
    btnRest.setOnClickListener(new View.OnClickListener() {  
      @Override 
      public void onClick(View v) {  
        chronometer.setBase(SystemClock.elapsedRealtime());  
 
      }  
 
    });  
    chronometer  
        .setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {  
          @Override 
          public void onChronometerTick(Chronometer chronometer) {  
            // 如果開(kāi)始計(jì)時(shí)到現(xiàn)在超過(guò)了startime秒  
            if (SystemClock.elapsedRealtime()  
                - chronometer.getBase() > startTime * 1000) {  
              chronometer.stop();  
              // 給用戶(hù)提示  
              showDialog();  
            }  
          }  
        });  
  }  
 
  protected void showDialog() {  
    AlertDialog.Builder builder = new AlertDialog.Builder(this);  
     builder.setIcon(R.drawable.eb28d25);  
    builder.setTitle("警告").setMessage("時(shí)間到")  
        .setPositiveButton("確定", new DialogInterface.OnClickListener() {  
          @Override 
          public void onClick(DialogInterface dialog, int which) {  
          }  
        });  
 
    AlertDialog dialog = builder.create();  
    dialog.show();  
  }  
} 

以上就是關(guān)于Android Chronometer控件實(shí)現(xiàn)計(jì)時(shí)器的相關(guān)函數(shù),希望對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。

相關(guān)文章

最新評(píng)論