質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

1876閲覧

CountDownTimer()で、cancelした時間を取得したい

imatya999

総合スコア38

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2021/10/09 22:57

編集2021/10/10 08:25

前提・実現したいこと

カウントダウンタイマーで
リスタートボタンを押したら、cancel()した続きから開始されるようにしたいので
経過時間を取得したい。(今は、毎回初期設定の時間に戻ってしまう。)

該当のソースコード

kotlin

1package com.example.sampletimer 2 3import androidx.appcompat.app.AppCompatActivity 4import android.os.Bundle 5import android.os.CountDownTimer 6import android.widget.Button 7import android.widget.TextView 8 9class MainActivity : AppCompatActivity() { 10 override fun onCreate(savedInstanceState: Bundle?) { 11 super.onCreate(savedInstanceState) 12 setContentView(R.layout.activity_main) 13 14 var allTime:Long = 10000 15 val textView: TextView =findViewById(R.id.textView) 16 val startButton:Button = findViewById(R.id.startButton) 17 val stopButton:Button=findViewById(R.id.stopButton) 18 val restartButton:Button=findViewById(R.id.restartButton) 19 20 21 val timer = object:CountDownTimer(allTime,100){ 22 23 override fun onTick(p0: Long) { 24 //TODO("Not yet implemented") 25 textView.text=p0.toString() 26 } 27 28 override fun onFinish() { 29 //TODO("Not yet implemented") 30 textView.text="タイムアップ" 31 } 32 } 33 34 //スタートボタン 35 startButton.setOnClickListener { 36 timer.start() 37 } 38 39 //ストップボタン 40 stopButton.setOnClickListener { 41 timer.cancel() 42 } 43 44 //リスタートボタン 45 restartButton.setOnClickListener { 46 //allTime= p0 47 timer.start() 48 } 49 } 50}

試したこと

リスタートボタンを押したときに「経過時間」を取得するには
onTickの「p0」らしい、というところまではわかりました。
ということで、それをalltimeに代入しようとしたのですが、アクセスできないため、
そこで書き方がわからず行き詰りました。

補足情報(FW/ツールのバージョンなど)

android studio Arctic Fox | 2020.3.1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hoshi-takanori

2021/10/10 00:33

CountDownTimer の残り時間を変更するには、新しく CountDownTimer を作り直すしかないようですね…。
imatya999

2021/10/10 08:38

ありがとうございました。
guest

回答1

0

ベストアンサー

~~ >それをalltimeに代入しようとしたのですが、アクセスできないため~~

alltime は onCreate 内にのみ実体のある変数ですから、onCreate が終われば消えます。 restartButton.setOnClickListener (に設定する OnClickListener オブジェクト) からはアクセスできないということではないでしょうか。

経過時間を保存する"フィールド"を用意し、 onTick が呼ばれる度に p0 をそのフィールドに保存しておき、リスタートボタン押下時に、そのフィールドの値から再度 CountDownTimer を設定・実行する形になると思います。
(CountDownTimer を再生成することになりますので、他のボタンとの関係上、 timer もフィールドにすることになりそうです。)


タイマーの生成・実行をローカル関数にしてみました。

kotlin

1package com.teratail.q363662 2 3import androidx.appcompat.app.AppCompatActivity 4import android.os.Bundle 5import android.os.CountDownTimer 6import android.widget.Button 7import android.widget.TextView 8 9class MainActivity : AppCompatActivity() { 10 companion object { 11 private const val DEFAULT_TIME: Long = 10000 //[ms] 12 } 13 14 override fun onCreate(savedInstanceState: Bundle?) { 15 super.onCreate(savedInstanceState) 16 setContentView(R.layout.activity_main) 17 18 var allTime:Long = 0 19 var timer: CountDownTimer? = null 20 21 val textView: TextView = findViewById(R.id.textView) 22 val startButton: Button = findViewById(R.id.startButton) 23 val stopButton: Button = findViewById(R.id.stopButton) 24 val restartButton: Button = findViewById(R.id.restartButton) 25 26 fun buttonsState(ready: Boolean) { 27 startButton.isEnabled = ready 28 stopButton.isEnabled = !ready 29 restartButton.isEnabled = ready && allTime > 0L 30 } 31 32 fun startCountDown(millisInFuture: Long): CountDownTimer { 33 return object:CountDownTimer(millisInFuture, 100) { 34 override fun onTick(millisUntilFinished: Long) { 35 allTime = millisUntilFinished 36 textView.text = millisUntilFinished.toString() 37 } 38 override fun onFinish() { 39 allTime = 0 40 textView.text = "タイムアップ" 41 buttonsState(true) 42 } 43 }.start() 44 } 45 46 buttonsState(true) 47 48 //スタートボタン 49 startButton.setOnClickListener { 50 timer = startCountDown(DEFAULT_TIME) 51 buttonsState(false) 52 } 53 54 //ストップボタン 55 stopButton.setOnClickListener { 56 timer?.cancel() 57 buttonsState(true) 58 } 59 60 //リスタートボタン 61 restartButton.setOnClickListener { 62 timer = startCountDown(allTime) 63 buttonsState(false) 64 } 65 } 66}

投稿2021/10/10 05:02

編集2021/10/10 08:02
jimbe

総合スコア12756

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hoshi-takanori

2021/10/10 05:27

Kotlin には Java と違ってクロージャがあるので、allTime への代入はできるはずですが、allTime の値を変更しても timer の時間は変更できないために期待通りの動きにならないと思います。
jimbe

2021/10/10 06:27 編集

おお、そうなんですね。つい java で考えてしまいました。 とりあえず(動作確認が出来たものが出来るまで?)消し線を入れます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問