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

Android時(shí)分秒計(jì)時(shí)器的兩種實(shí)現(xiàn)方法

 更新時(shí)間:2016年08月24日 15:06:44   作者:南塵  
這篇文章主要介紹了Android時(shí)分秒計(jì)時(shí)器的兩種實(shí)現(xiàn)方法,分別是Chronometer控件和handler+timer+timerTask方式,非常不錯,感興趣的朋友一起看下吧

可能我們在開發(fā)中會時(shí)常用到計(jì)時(shí)器這玩意兒,比如在錄像的時(shí)候,我們可能需要在右上角顯示一個(gè)計(jì)時(shí)器。這個(gè)東西其實(shí)實(shí)現(xiàn)起來非常簡單。

只需要用一個(gè)控件Chronometer,是的,就這么簡單,我都不好意思講述一下了。

<Chronometer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format="%s"
android:id="@+id/timer"/>

是的,就這么簡單。java代碼同樣

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer = (Chronometer) findViewById(R.id.timer);
}
public void btnClick(View view) {
timer.setBase(SystemClock.elapsedRealtime());//計(jì)時(shí)器清零
timer.start();
}

超簡單有木有?看看運(yùn)行結(jié)果:

或許你會說,這個(gè)要是需要顯示上時(shí)間怎么弄呢?不急不急,兩行代碼就能解決的事情。

public void btnClick(View view) {
timer.setBase(SystemClock.elapsedRealtime());//計(jì)時(shí)器清零
int hour = (int) ((SystemClock.elapsedRealtime() - timer.getBase()) / 1000 / 60);
timer.setFormat("0"+String.valueOf(hour)+":%s");
timer.start();
}
public void stopClick(View view) {
timer.stop();
}

恩,對,就是 這么簡單,不過別忘了把xml的format改一下

<Chronometer
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:format="00:00:00"
android:gravity="center"
android:id="@+id/timer"/>

是的,你沒有看錯,這樣就可以了,不信,你看!

就和你想象的錄像上方的時(shí)間一樣有木有?恩。你前面設(shè)置一個(gè)圓圈,再設(shè)置計(jì)時(shí)器顏色就和它一樣有逼格了。

而或許你并不喜歡用這種方式,當(dāng)然用handler+timer+timerTask的方式也是可以的啦。由于太簡單,就直接上代碼了。

package com.example.nanchen.timerdemo;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Chronometer;
import android.widget.TextView;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private Chronometer timer;
private Timer timer1;
private TextView textView;
private TimerTask timerTask;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer = (Chronometer) findViewById(R.id.timer);
textView = (TextView) findViewById(R.id.text);
timer1 = new Timer();
}
public void btnClick(View view) {
timer.setBase(SystemClock.elapsedRealtime());//計(jì)時(shí)器清零
int hour = (int) ((SystemClock.elapsedRealtime() - timer.getBase()) / 1000 / 60);
timer.setFormat("0"+String.valueOf(hour)+":%s");
timer.start();
}
public void stopClick(View view) {
timer.stop();
}
public void startClick(View view) {
timerTask = new TimerTask() {
int cnt = 0;
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(getStringTime(cnt++));
}
});
}
};
timer1.schedule(timerTask,0,1000);
}
private String getStringTime(int cnt) {
int hour = cnt/3600;
int min = cnt % 3600 / 60;
int second = cnt % 60;
return String.format(Locale.CHINA,"%02d:%02d:%02d",hour,min,second);
}
public void stopClick1(View view) {
if (!timerTask.cancel()){
timerTask.cancel();
timer1.cancel();
}
}
}
<?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="com.example.nanchen.timerdemo.MainActivity">
<Chronometer
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:format="00:00:00"
android:gravity="center"
android:id="@+id/timer"/>
<Button
android:layout_width="match_parent"
android:onClick="btnClick"
android:text="start"
android:layout_height="wrap_content"/>
<Button
android:layout_width="match_parent"
android:text="stop"
android:onClick="stopClick"
android:layout_height="wrap_content"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#959393"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="00:00:00"
android:gravity="center"
android:id="@+id/text"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="開始"
android:onClick="startClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止"
android:onClick="stopClick1"/>
</LinearLayout>

簡單運(yùn)行下方用timer實(shí)現(xiàn)的效果:

想必大家到這樣都會有了自己的理解,android 官方的Chronometer方式只是為了做一個(gè)計(jì)時(shí)器,而我們采用自己用Timer和TimerTask方式可以更加自主,因?yàn)槟憧梢韵霃氖裁磿r(shí)間開始計(jì)時(shí)就從什么時(shí)間開始計(jì)時(shí),計(jì)時(shí)方式想順計(jì)時(shí)倒計(jì)時(shí)都不是難事兒,甚至各種浮夸的隔兩秒,隔三秒,隔n秒都是可以的,具體使用就看你選擇咯~~

以上所述是小編給大家介紹的Android時(shí)分秒計(jì)時(shí)器的兩種實(shí)現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Android zygote啟動流程詳解

    Android zygote啟動流程詳解

    這篇文章主要介紹了Android zygote啟動流程的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下
    2021-03-03
  • 利用Flutter繪制出3D效果動畫

    利用Flutter繪制出3D效果動畫

    本文主要介紹了Flutter繪圖的Path的應(yīng)用。Flutter的Path類提供了一個(gè)三維空間的變換方法,可以實(shí)現(xiàn)路徑在三維空間的平移、旋轉(zhuǎn)等操作,從而可以實(shí)現(xiàn)3D繪制的效果,感興趣的可以了解一下
    2022-08-08
  • Android添加指紋解鎖功能的實(shí)現(xiàn)代碼

    Android添加指紋解鎖功能的實(shí)現(xiàn)代碼

    當(dāng)開發(fā)的APP需要加密驗(yàn)證時(shí)可以考慮添加系統(tǒng)指紋解鎖功能。這篇文章主要介紹了Android添加指紋解鎖功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2018-07-07
  • Android模擬器安裝APP出現(xiàn)INSTALL_FAILED_NO_MATCHING_ABIS錯誤解決方案

    Android模擬器安裝APP出現(xiàn)INSTALL_FAILED_NO_MATCHING_ABIS錯誤解決方案

    這篇文章主要介紹了 Android模擬器安裝APP出現(xiàn)INSTALL_FAILED_NO_MATCHING_ABIS錯誤解決方案的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • Android方向傳感器的使用方法

    Android方向傳感器的使用方法

    這篇文章主要介紹了Android方向傳感器的使用方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-10-10
  • html5在android中的使用問題及技巧解讀

    html5在android中的使用問題及技巧解讀

    本文將詳細(xì)介紹下html5在android中的使用:特效按鈕的進(jìn)展/在html5中有關(guān)于觸摸屏的事件,感興趣的你可以參考下,或許對你有所幫助
    2013-03-03
  • Android VelocityTracker使用案例詳解

    Android VelocityTracker使用案例詳解

    這篇文章主要介紹了Android VelocityTracker使用案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Android開發(fā)之TabActivity用法實(shí)例詳解

    Android開發(fā)之TabActivity用法實(shí)例詳解

    這篇文章主要介紹了Android開發(fā)之TabActivity用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android擴(kuò)展Activity實(shí)現(xiàn)標(biāo)簽頁效果的具體步驟與相關(guān)技巧,需要的朋友可以參考下
    2016-03-03
  • Android仿微信布局的實(shí)現(xiàn)示例

    Android仿微信布局的實(shí)現(xiàn)示例

    本文主要介紹了Android仿微信布局的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Android源碼中常用的接口傳參實(shí)例詳解

    Android源碼中常用的接口傳參實(shí)例詳解

    這篇文章主要介紹了Android源碼中常用的接口傳參實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04

最新評論