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

Android入門之計時器Chronometer的使用教程

 更新時間:2022年11月11日 09:24:28   作者:TGITCIC  
Chronometer是一個簡單的定時器,你可以給它一個開始時間,并以此定時。本文將利用個簡單的示例為大家講解一下它的使用,感興趣的小伙伴可以嘗試一下

介紹

非常簡單的一個計時器,沒有太多原理,我們直接上代碼。

先看課程目標(biāo)

課程目標(biāo)

就是一個簡單的計時器,我們直接上使用示例吧

界面里有一個計時器,4個按鈕。

  • 開始計時,上面這個計時器就開始讀秒;
  • 停止計時,計時器會暫停計時;
  • 重置,計時器會歸零;
  • 變格式,計時器會變成:Time:%s"的格式顯示;

界面端代碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <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="開始記時" />
 
        <Button
            android:id="@+id/btnStop"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="停止記時" />
 
        <Button
            android:id="@+id/btnReset"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="重置" />
 
        <Button
            android:id="@+id/btnFormat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="變格式" />
    </LinearLayout>
 
</LinearLayout>

后端交互代碼

package org.mk.android.demo.demochrometer;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
 
public class MainActivity extends AppCompatActivity {
    private Chronometer chronometer;
    private Button btnStart,btnStop,btnReset,btnFormat;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnFormat=(Button) findViewById(R.id.btnFormat);
        btnStart=(Button) findViewById(R.id.btnStart);
        btnStop=(Button) findViewById(R.id.btnStop);
        btnReset=(Button) findViewById(R.id.btnReset);
        btnStart.setOnClickListener(new OnClickListener());
        btnStop.setOnClickListener(new OnClickListener());
        btnReset.setOnClickListener(new OnClickListener());
        btnFormat.setOnClickListener(new OnClickListener());
        chronometer=(Chronometer) findViewById(R.id.chronometer);
    }
    private class OnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btnStart:
                    chronometer.start();// 開始計時
                    break;
                case R.id.btnStop:
                    chronometer.stop();// 停止計時
                    break;
                case R.id.btnReset:
                    chronometer.setBase(SystemClock.elapsedRealtime());// 復(fù)位
                    break;
                case R.id.btnFormat:
                    Log.i("app","into formatter");
                    chronometer.setFormat("Time:%s");// 更改時間顯示格式
                    break;
            }
        }
    }
}

運(yùn)行效果

以上是按下了【變格式】按鈕后顯示的變化,自己去動動手試一下唄。

到此這篇關(guān)于Android入門之計時器Chronometer的使用教程的文章就介紹到這了,更多相關(guān)Android計時器Chronometer內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論