亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android入門之計(jì)時(shí)器Chronometer的使用教程

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

介紹

非常簡(jiǎn)單的一個(gè)計(jì)時(shí)器,沒有太多原理,我們直接上代碼。

先看課程目標(biāo)

課程目標(biāo)

就是一個(gè)簡(jiǎn)單的計(jì)時(shí)器,我們直接上使用示例吧

界面里有一個(gè)計(jì)時(shí)器,4個(gè)按鈕。

  • 開始計(jì)時(shí),上面這個(gè)計(jì)時(shí)器就開始讀秒;
  • 停止計(jì)時(shí),計(jì)時(shí)器會(huì)暫停計(jì)時(shí);
  • 重置,計(jì)時(shí)器會(huì)歸零;
  • 變格式,計(jì)時(shí)器會(huì)變成: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="開始記時(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="重置" />
 
        <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();// 開始計(jì)時(shí)
                    break;
                case R.id.btnStop:
                    chronometer.stop();// 停止計(jì)時(shí)
                    break;
                case R.id.btnReset:
                    chronometer.setBase(SystemClock.elapsedRealtime());// 復(fù)位
                    break;
                case R.id.btnFormat:
                    Log.i("app","into formatter");
                    chronometer.setFormat("Time:%s");// 更改時(shí)間顯示格式
                    break;
            }
        }
    }
}

運(yùn)行效果

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

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

相關(guān)文章

最新評(píng)論