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

Android開(kāi)發(fā)實(shí)現(xiàn)的計(jì)時(shí)器功能示例

 更新時(shí)間:2019年04月04日 11:59:47   作者:水中魚(yú)之1999  
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)的計(jì)時(shí)器功能,涉及Android開(kāi)發(fā)中的計(jì)時(shí)器相關(guān)組件布局、調(diào)用、事件響應(yīng)等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android開(kāi)發(fā)實(shí)現(xiàn)的計(jì)時(shí)器功能。分享給大家供大家參考,具體如下:

效果圖:

布局:

三個(gè)按鈕 加上一個(gè)Chronometer

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity"
  android:orientation="vertical"
  android:gravity="center_horizontal">
  <Chronometer
    android:id="@+id/test"
    android:textSize="25pt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
      android:id="@+id/start"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="開(kāi)始"
      android:layout_weight="1"/>
    <Button
      android:id="@+id/pause"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="暫停"
      android:layout_weight="1"/>
    <Button
      android:id="@+id/go_on"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="繼續(xù)"
      android:layout_weight="1"/>
  </LinearLayout>
</LinearLayout>

實(shí)現(xiàn):

四個(gè)監(jiān)聽(tīng)事件 三個(gè)按鈕 一個(gè)計(jì)時(shí)器

package com.example.a30797.androidtest;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
public class MainActivity extends AppCompatActivity {
  Chronometer ch ;
  Button start ;
  Button pause ;
  Button restart ;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //獲取計(jì)時(shí)器組件
    ch = (Chronometer) findViewById(R.id.test);
    //獲取開(kāi)始按鈕
    start = (Button) findViewById(R.id.start) ;
    //暫停計(jì)時(shí)按鈕
    pause = (Button) findViewById(R.id.pause);
    //繼續(xù)計(jì)時(shí)按鈕
    restart = (Button) findViewById(R.id.go_on);
    start.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        //設(shè)置開(kāi)始計(jì)時(shí)時(shí)間
        ch.setBase(SystemClock.elapsedRealtime() );
        //啟動(dòng)計(jì)時(shí)器
        ch.start();
        pause.setEnabled(true);
        restart.setEnabled(false);
        start.setEnabled(false);
      }
    });
    //暫停按鈕監(jiān)聽(tīng)器
    pause.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        start.setText("重新開(kāi)始");
        ch.stop();
        start.setEnabled(true);
        restart.setEnabled(true);
        pause.setEnabled(false);
      }
    });
    //暫停按鈕監(jiān)聽(tīng)器
    restart.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        start.setText("重新開(kāi)始");
        ch.start();
        start.setEnabled(true);
        pause.setEnabled(true);
        restart.setEnabled(false);
      }
    });
    //為Chronomter綁定事件監(jiān)聽(tīng)器
    ch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
      @Override
      public void onChronometerTick(Chronometer chronometer) {
        //如果計(jì)時(shí)到現(xiàn)在超過(guò)了一小時(shí)秒
        if ( SystemClock.elapsedRealtime() - ch.getBase() > 3600 * 1000) {
          ch.stop();
          start.setEnabled(true);
          restart.setEnabled(false);
          pause.setEnabled(false);
        }
      }
    });
  }
}

PS:這里再為大家推薦幾款相關(guān)的在線工具供大家參考:

在線秒表工具:
http://tools.jb51.net/bianmin/miaobiao

Unix時(shí)間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android日期與時(shí)間操作技巧總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論