最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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í)器,沒(méi)有太多原理,我們直接上代碼。

先看課程目標(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)文章

  • Android應(yīng)用內(nèi)存泄漏優(yōu)化指南

    Android應(yīng)用內(nèi)存泄漏優(yōu)化指南

    內(nèi)存泄漏(Memory Leak)是指應(yīng)用中不再使用的對(duì)象因錯(cuò)誤引用無(wú)法被垃圾回收(GC),導(dǎo)致內(nèi)存占用持續(xù)增長(zhǎng),最終可能引發(fā) OOM(Out Of Memory)崩潰?或 應(yīng)用卡頓,以下是 Android 內(nèi)存泄漏的優(yōu)化方案,涵蓋檢測(cè)工具、常見場(chǎng)景及解決方案,需要的朋友可以參考下
    2025-03-03
  • 安卓圖片反復(fù)壓縮后為什么普遍會(huì)變綠而不是其它顏色?

    安卓圖片反復(fù)壓縮后為什么普遍會(huì)變綠而不是其它顏色?

    今天小編就為大家分享一篇關(guān)于安卓圖片反復(fù)壓縮后為什么普遍會(huì)變綠而不是其它顏色?,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • Android 解決游戲發(fā)行切包資源索引沖突的問(wèn)題

    Android 解決游戲發(fā)行切包資源索引沖突的問(wèn)題

    這篇文章主要介紹了Android 解決游戲發(fā)行切包資源索引沖突的問(wèn)題,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-03-03
  • Android編程自定義圓角半透明Dialog的方法

    Android編程自定義圓角半透明Dialog的方法

    這篇文章主要介紹了Android編程自定義圓角半透明Dialog的方法,涉及Android控件的布局及相關(guān)屬性設(shè)置技巧,需要的朋友可以參考下
    2017-03-03
  • Flutter實(shí)現(xiàn)頁(yè)面切換后保持原頁(yè)面狀態(tài)的3種方法

    Flutter實(shí)現(xiàn)頁(yè)面切換后保持原頁(yè)面狀態(tài)的3種方法

    這篇文章主要給大家介紹了關(guān)于Flutter實(shí)現(xiàn)頁(yè)面切換后保持原頁(yè)面狀態(tài)的3種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Android字符串資源文件format方法使用實(shí)例

    Android字符串資源文件format方法使用實(shí)例

    本文介紹了Android的資源文件values/strings.xml中如何實(shí)現(xiàn)格式化字符串,這里舉個(gè)簡(jiǎn)單的例子供大家參考
    2013-11-11
  • Android編程下拉菜單spinner用法小結(jié)(附2則示例)

    Android編程下拉菜單spinner用法小結(jié)(附2則示例)

    這篇文章主要介紹了Android編程下拉菜單spinner用法,結(jié)合實(shí)例較為詳細(xì)的總結(jié)分析了下拉菜單Spinner的具體實(shí)現(xiàn)步驟與相關(guān)技巧,并附帶兩個(gè)示例分析其具體用法,需要的朋友可以參考下
    2015-12-12
  • Android檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的方法

    Android檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的方法

    這篇文章主要為大家詳細(xì)介紹了Android檢測(cè)手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android使用屬性動(dòng)畫如何自定義倒計(jì)時(shí)控件詳解

    Android使用屬性動(dòng)畫如何自定義倒計(jì)時(shí)控件詳解

    自Android 3.0版本開始,系統(tǒng)給我們提供了一種全新的動(dòng)畫模式,屬性動(dòng)畫(property animation),它的功能非常強(qiáng)大,下面這篇文章主要給大家介紹了關(guān)于Android使用屬性動(dòng)畫如何自定義倒計(jì)時(shí)控件的相關(guān)資料,需要的朋友可以參考下
    2018-05-05
  • Android五子棋游戲程序完整實(shí)例分析

    Android五子棋游戲程序完整實(shí)例分析

    這篇文章主要為大家分享了Android五子棋游戲程序完整實(shí)例,內(nèi)容豐富,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-04-04

最新評(píng)論

东乡族自治县| 尼勒克县| 甘南县| 绩溪县| 金寨县| 长岛县| 翁源县| 汝阳县| 荣成市| 临洮县| 仁怀市| 徐闻县| 泰安市| 收藏| 曲松县| 古田县| 静乐县| 安顺市| 方正县| 旺苍县| 南汇区| 五指山市| 镇江市| 平远县| 侯马市| 仙游县| 灵武市| 绥滨县| 信丰县| 平远县| 错那县| 且末县| 山阴县| 七台河市| 宣恩县| 峨眉山市| 顺平县| 乌兰察布市| 肇庆市| 象州县| 珠海市|