Androidは簡単なタイマー機能を実現します。


本論文の例では、Androidが簡単なタイマーを実現するための具体的なコードを共有しています。
レイアウト
res/layoutでレイアウトする

<?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:id="@+id/timeView"/>
 <Button
  android:text="start"
  android:onClick="onClickStart"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" android:id="@+id/start"/>
 <Button
  android:text="stop"
  android:onClick="onClickStop"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" android:id="@+id/stop"/>
 <Button
  android:text="reset"
  android:onClick="onClickReset"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" android:id="@+id/reset"/>
 </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; //    
 private boolean wasRunning = false; //  running   

 //app    ,    
 @Override
 protected void onStop() {
 super.onStop();
 wasRunning = running;
 running = false;
 }

 //    app,    
 @Override
 protected void onStart() {
 super.onStart();
 if(wasRunning) running = true;
 }

 //    (   ),    
 @Override
 protected void onPause() {
 super.onPause();
 wasRunning = running;
 running = false;
 }

 //    ,      
 @Override
 protected void onResume() {
 super.onResume();
 if(wasRunning) running = true;
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 //       
 if(savedInstanceState!=null){
  seconds = savedInstanceState.getInt("seconds");
  running = savedInstanceState.getBoolean("running");
  wasRunning = savedInstanceState.getBoolean("wasRunning");
 }
 runTime();
 }

 /**
 *    
 */
 @Override
 public void onSaveInstanceState(Bundle saveInstanceState) {

 super.onSaveInstanceState(saveInstanceState);
 saveInstanceState.putInt("seconds",seconds);
 saveInstanceState.putBoolean("running",running);
 saveInstanceState.putBoolean("wasRunning",wasRunning);
 }
 /**
 *   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     sleep  
 *    ui      ,           
 *
 * **   ui   Handler     **
 *        Runnable run    ,  Handler 
 * post(      ) postDelayed(      )     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);
  }
  }
 );

 }
}
テスト

完了
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。