【问题标题】:how to make circular progress count down timer with having Play,Pause and Reset buttton in kotline如何使用 kotlin 中的播放、暂停和重置按钮使循环进度倒计时
【发布时间】:2023-02-24 02:22:57
【问题描述】:

使用共享偏好

如何使用 kotlin 中的播放、暂停和重置按钮使循环进度倒计时

【问题讨论】:

    标签: android


    【解决方案1】:

    类 MainActivity : AppCompatActivity() {

    private lateinit var progressBar: ProgressBar
    private lateinit var textView: TextView
    private lateinit var playButton: Button
    private lateinit var pauseButton: Button
    private lateinit var resetButton: Button
    private lateinit var countDownTimer: CountDownTimer
    
    private var totalTime = 60000L //total countdown time in milliseconds
    private var timeLeft = totalTime
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    
        progressBar = findViewById(R.id.progressBar)
        textView = findViewById(R.id.textView)
        playButton = findViewById(R.id.playButton)
        pauseButton = findViewById(R.id.pauseButton)
        resetButton = findViewById(R.id.resetButton)
    
        progressBar.max = totalTime.toInt()
    
        countDownTimer = object : CountDownTimer(totalTime, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                timeLeft = millisUntilFinished
                updateTimer()
            }
    
            override fun onFinish() {
                timeLeft = 0
                updateTimer()
            }
        }
    
        playButton.setOnClickListener {
            countDownTimer.start()
        }
    
        pauseButton.setOnClickListener {
            countDownTimer.cancel()
        }
    
        resetButton.setOnClickListener {
            countDownTimer.cancel()
            timeLeft = totalTime
            updateTimer()
            progressBar.progress = totalTime.toInt()
        }
    }
    
    private fun updateTimer() {
        val minutes = (timeLeft / 1000) / 60
        val seconds = (timeLeft / 1000) % 60
        val timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds)
        textView.text = timeLeftFormatted
        progressBar.progress = (totalTime - timeLeft).toInt()
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      • 1970-01-01
      • 2022-06-26
      相关资源
      最近更新 更多