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

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;
            }
        }
    }
}

運行效果

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

到此這篇關(guān)于Android入門之計時器Chronometer的使用教程的文章就介紹到這了,更多相關(guān)Android計時器Chronometer內(nèi)容請搜索腳本之家以前的文章或繼續(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)用中不再使用的對象因錯誤引用無法被垃圾回收(GC),導(dǎo)致內(nèi)存占用持續(xù)增長,最終可能引發(fā) OOM(Out Of Memory)崩潰?或 應(yīng)用卡頓,以下是 Android 內(nèi)存泄漏的優(yōu)化方案,涵蓋檢測工具、常見場景及解決方案,需要的朋友可以參考下
    2025-03-03
  • 安卓圖片反復(fù)壓縮后為什么普遍會變綠而不是其它顏色?

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

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

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

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

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

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

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

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

    Android字符串資源文件format方法使用實例

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

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

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

    Android檢測手機(jī)多點觸摸點數(shù)的方法

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

    Android使用屬性動畫如何自定義倒計時控件詳解

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

    Android五子棋游戲程序完整實例分析

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

最新評論

从江县| 保亭| 井陉县| 嘉荫县| 南皮县| 福建省| 西吉县| 深州市| 安远县| 蕲春县| 屯门区| 镇平县| 英吉沙县| 阿尔山市| 澄江县| 南京市| 常德市| 醴陵市| 乌拉特前旗| 中牟县| 阆中市| 临高县| 开阳县| 康乐县| 吴堡县| 安岳县| 绵竹市| 白玉县| 大埔县| 怀宁县| 平阴县| 金山区| 凤山市| 儋州市| 新沂市| 呼和浩特市| 丹寨县| 开化县| 万荣县| 察雅县| 洮南市|