国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Android實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器功能

瀏覽:89日期:2022-09-22 09:01:33

本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器的具體代碼,供大家參考,具體內(nèi)容如下

布局

在res/layout 下進(jìn)行布局

<?xml version='1.0' encoding='utf-8'?><android.support.constraint.ConstraintLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' xmlns:app='http://schemas.android.com/apk/res-auto' android:layout_width='match_parent' android:layout_height='match_parent' tools:context='.MainActivity'> <LinearLayout android:orientation='vertical' android:layout_width='match_parent' android:layout_height='match_parent' tools:layout_editor_absoluteY='0dp' tools:layout_editor_absoluteX='0dp'> <TextView android:text='00:00:00' android:textSize='60sp' android:gravity='center' android:layout_width='match_parent' android:layout_height='wrap_content' android: /> <Button android:text='start' android:onClick='onClickStart' android:layout_width='match_parent' android:layout_height='wrap_content' android: /> <Button android:text='stop' android:onClick='onClickStop' android:layout_width='match_parent' android:layout_height='wrap_content' android: /> <Button android:text='reset' android:onClick='onClickReset' android:layout_width='match_parent' android:layout_height='wrap_content' android: /> </LinearLayout></android.support.constraint.ConstraintLayout>

MainActivity

package com.test;import android.os.Handler;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView;public class MainActivity extends AppCompatActivity { private int seconds = 0; private boolean running = false; //計(jì)時(shí)狀態(tài) private boolean wasRunning = false; //保存running的狀態(tài) //app進(jìn)入后臺(tái),暫停計(jì)時(shí) @Override protected void onStop() { super.onStop(); wasRunning = running; running = false; } //重新進(jìn)入app,開始計(jì)時(shí) @Override protected void onStart() { super.onStart(); if(wasRunning) running = true; } //失去焦點(diǎn)(如分屏),暫停計(jì)時(shí) @Override protected void onPause() { super.onPause(); wasRunning = running; running = false; } //獲得焦點(diǎn),重新開始計(jì)時(shí) @Override protected void onResume() { super.onResume(); if(wasRunning) running = true; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取保存的狀態(tài) if(savedInstanceState!=null){ seconds = savedInstanceState.getInt('seconds'); running = savedInstanceState.getBoolean('running'); wasRunning = savedInstanceState.getBoolean('wasRunning'); } runTime(); } /** *保存狀態(tài) */ @Override public void onSaveInstanceState(Bundle saveInstanceState) { super.onSaveInstanceState(saveInstanceState); saveInstanceState.putInt('seconds',seconds); saveInstanceState.putBoolean('running',running); saveInstanceState.putBoolean('wasRunning',wasRunning); } /** * 響應(yīng)button的onClick事件 * 方法名和onClick的值一致 */ public void onClickStart(View button){ running = true; } public void onClickStop(View button){ running = false; } public void onClickReset(View button){ running = false; seconds = 0; } /** * 注意 ui線程不能被堵塞,因此不能在ui線程中調(diào)用sleep方法 * 只允許ui線程更新界面,不能在后臺(tái)線程更新界面 * * ** 使用ui線程的Handler定時(shí)更新 ** * 將任務(wù)封裝到 Runnable的run方法中 ,通過Handler的 * post(立即提交任務(wù))或postDelayed(實(shí)現(xiàn)定時(shí)調(diào)度)方法提交到ui線程 */ private void runTime(){ final Handler handler = new Handler(); handler.post(new Runnable() { @Override public void run() { final TextView textView = findViewById(R.id.timeView); int hour = seconds /3600%24; int minute = seconds%3600/60; String time = String.format('%02d:%02d:%02d',hour,minute,seconds%60); textView.setText(time); if(running) seconds++; handler.postDelayed(this,1000); } } ); }}

測(cè)試

Android實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器功能

完成

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 金阳县| 景泰县| 当阳市| 甘南县| 麦盖提县| 泰宁县| 蓬安县| 涟源市| 三明市| 灵山县| 尼玛县| 南召县| 潜山县| 吉安县| 隆林| 北流市| 罗江县| 宽城| 黄骅市| 黑山县| 黄山市| 呼和浩特市| 乌拉特前旗| 无锡市| 平遥县| 新巴尔虎右旗| 武宣县| 汉中市| 雷波县| 浮梁县| 肇东市| 孟连| 溧水县| 三台县| 英超| 平阴县| 海宁市| 修文县| 开江县| 桑植县| 宜昌市|