前提・実現したいこと
カウントダウンタイマーの開始タイミングを画面遷移時にしたいです。
以下のサイトを参考に5秒のカウントダウンタイマーを作っています。
https://akira-watson.com/android/countdowntimer.html
このページではstartbuttonのonClickからカウントダウンを始めていますが、今回はstartButtonを無くしてactivity mainが画面遷移により表示された2秒後の開始にしたいと考えています。その後のStopに関してはそのままです。
どのような手法が考えられますでしょうか。
アドバイスお願いします。
MainActivity
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private TextView timerText; private SimpleDateFormat dataFormat = new SimpleDateFormat("mm:ss.SSS", Locale.US); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 3分= 3x60x1000 = 180000 msec long countNumber = 180000; // インターバル msec long interval = 10; Button startButton = findViewById(R.id.start_button); Button stopButton = findViewById(R.id.stop_button); timerText = findViewById(R.id.timer); timerText.setText(dataFormat.format(0)); // インスタンス生成 // CountDownTimer(long millisInFuture, long countDownInterval) final CountDown countDown = new CountDown(countNumber, interval); startButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 開始 countDown.start(); } }); stopButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { // 中止 countDown.cancel(); timerText.setText(dataFormat.format(0)); } }); } class CountDown extends CountDownTimer { CountDown(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onFinish() { // 完了 timerText.setText(dataFormat.format(0)); } // インターバルで呼ばれる @Override public void onTick(long millisUntilFinished) { // 残り時間を分、秒、ミリ秒に分割 //long mm = millisUntilFinished / 1000 / 60; //long ss = millisUntilFinished / 1000 % 60; //long ms = millisUntilFinished - ss * 1000 - mm * 1000 * 60; //timerText.setText(String.format("%1$02d:%2$02d.%3$03d", mm, ss, ms)); timerText.setText(dataFormat.format(millisUntilFinished)); } }
}
Layout
<?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:padding="20dp" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity">
</LinearLayout><TextView android:id="@+id/timer" android:layout_margin="20dp" android:textSize="60sp" android:textColor="#00f" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="@string/start" android:id="@+id/start_button" android:layout_margin="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="@string/stop" android:id="@+id/stop_button" android:layout_margin="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" />
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/05 10:44