初めての投稿になります。
Android Studio(Java使用)を使って、カウントダウンタイマーのアプリを作っています。
タイマーの主な機能は、条件分岐により、3分・4分・5分のタイム設定を選択してカウントダウンを行うというものです。
onClickとCountDownTimerによって各ボタン(タイム選択3パターン、スタート、ストップ)及びそれらの処理をしており、タイムを選択した上でカウントダウンをスタートすることはできます。
しかし、様々なサイトで調べながら解決法を探ったのですが、cancel()によってカウントダウンを途中で止めることができません。
どのようにしたら、カウントダウンを途中で停止できるのでしょうか。
よろしくお願いいたします。
該当のソースコード
Java
1public class Timer extends Activity implements View.OnClickListener { 2 3 //タイム表示変数 4 private TextView timer; 5 6 //時刻関係 7 private SimpleDateFormat dateformat = new SimpleDateFormat("mm:ss", Locale.US); 8 9 //タイムカウンター変数 10 long count_time; 11 12 //タイムインターバル 13 long interval = 10; 14 15 //各ボタン変数宣言 16 Button button1; 17 Button button2; 18 Button button3; 19 Button start_button; 20 Button stop_button; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_timer); 26 27 //タイムセットボタン(3分・4分・5分) 28 button1 = (Button) findViewById(R.id.button1); //3分 29 button2 = (Button) findViewById(R.id.button2); //4分 30 button3 = (Button) findViewById(R.id.button3); //5分 31 32 //スタート・ストップボタン 33 start_button = (Button) findViewById(R.id.start_button); 34 stop_button = (Button) findViewById(R.id.stop_button); 35 36 //リスナー設定 37 button1.setOnClickListener((View.OnClickListener) this); 38 button2.setOnClickListener((View.OnClickListener) this); 39 button3.setOnClickListener((View.OnClickListener) this); 40 41 //タイマー部分表示 42 timer = findViewById(R.id.timer_display); 43 timer.setText(dateformat.format(0)); 44 } 45 46 //タイムセットボタン動作処理 47 @Override 48 public void onClick(View view) { 49 int id = view.getId(); 50 switch (id) { 51 case R.id.button1: 52 count_time = 180000; //3 min = 3*60*1000 msec 53 timer.setText(dateformat.format(count_time)); 54 break; 55 case R.id.button2: 56 count_time = 240000; //4 min = 4*60*1000 msec 57 timer.setText(dateformat.format(count_time)); 58 break; 59 case R.id.button3: 60 count_time = 300000; //5 min = 5*60*1000 msec 61 timer.setText(dateformat.format(count_time)); 62 break; 63 } 64 //スタート・ストップボタンクラスリスナー 65 start_button.setOnClickListener(new StartButtonClick()); 66 stop_button.setOnClickListener(new StopButtonClick()); 67 } 68 69 //スタート・ストップボタン動作クラス 70 class StartButtonClick implements View.OnClickListener 71 { 72 //クラスCountDownのインスタンス 73 final CountDown counter = new CountDown(count_time, interval); 74 @Override 75 public void onClick(View v) 76 { 77 if(v.getId() == R.id.start_button) 78 { 79 counter.start(); 80 } 81 } 82 } 83 class StopButtonClick implements View.OnClickListener 84 { 85 final CountDown counter = new CountDown(count_time, interval); 86 @Override 87 public void onClick(View v) { 88 if (v.getId() == R.id.stop_button) { 89 counter.cancel(); 90 timer.setText(dateformat.format(0)); 91 } 92 } 93 } 94 95 //カウントダウン処理クラス 96 class CountDown extends CountDownTimer { 97 98 CountDown(long millisInFuture, long countDownInterval) { 99 super(millisInFuture, countDownInterval); 100 } 101 //カウント終了 102 public void onFinish() { 103 timer.setText(dateformat.format(0)); 104 } 105 //インターバル 106 public void onTick(long millisUntilFinished) { 107 timer.setText(dateformat.format(millisUntilFinished)); 108 } 109 } 110}
補足情報(FW/ツールのバージョンなど)
Android Studio 3使用
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/26 12:50