如何将CountDownTimer放入循环中?

时间:2019-12-26 03:33:31

标签: loops kotlin countdowntimer

因此,我目前正在开发一个应用程序,该应用程序将执行根据用户在文件中提供的输入来设计的任务。从输入中,应用程序将获得一个String的ArrayList,其大小将用于确定循环的重复次数。

任务是隐藏ImageView和两个按钮达特定的持续时间,然后使其再次可见(使用CountDownTimer)。当可见时,按钮会在用户单击任一按钮时从用户那里得到响应,并根据所单击的按钮存储一个值。一定时间后,ImageView和按钮将再次不可见。

循环将执行将执行任务的功能。我现在没有循环的代码如下:

    val timer3 = object : CountDownTimer(5000, 1000) {
        override fun onFinish() {
            visible() //ImageView and buttons to be visible
            btnX.setOnClickListener {
                val intentResultActivity = Intent(this@TaskActivity, ResultActivity::class.java)
                startActivity(intentResultActivity) //Go to the next activity
            }
            btnO.setOnClickListener {
                val intentResultActivity = Intent(this@TaskActivity, ResultActivity::class.java)
                startActivity(intentResultActivity) //Go to the next activity
            }
        }

        override fun onTick(p2: Long) {
            invisible() //ImageView and buttons to be invisible
            imageView.setImageURI(Uri.parse(SettingsActivity.pic3)) //Set the image 
        }
    }

    val timer2 = object : CountDownTimer(5000, 1000) {
        override fun onFinish() {
            visible() //ImageView and buttons to be visible
            btnX.setOnClickListener {
                timer3.start() //Start the next timer
            }
            btnO.setOnClickListener {
                timer3.start() //Start the next timer
            }
        }

        override fun onTick(p1: Long) {
            invisible() //ImageView and buttons to be invisible
            imageView.setImageURI(Uri.parse(SettingsActivity.pic2)) //Set the image 
        }
    }

    val timer = object: CountDownTimer(5000,1000){
        override fun onFinish() {
            visible() //ImageView and buttons to be visible
            btnX.setOnClickListener {
                timer2.start() //Start the next timer
            }
            btnO.setOnClickListener {
                timer2.start() //Start the next timer
            }
        }

        override fun onTick(p0: Long) {
            invisible() //ImageView and buttons to be invisible
            imageView.setImageURI(Uri.parse(SettingsActivity.pic1)) //Set the image 
        }
    }

我在下面执行的循环代码将执行最后一个CountDownTimer,并跳过其余部分。

    for (i in 0 until SettingsActivity.picArray.size) {
        if (i<(SettingsActivity.picArray.size-1)) {
            timerLoopBlank(i)
        }
    }

以下是在循环中执行任务的函数:

    private fun timerLoopBlank (i:Int) {
        val timerBlank = object: CountDownTimer(5000,1000){
            override fun onFinish() {
                visible()
                timerLoopAnswer()
            }

            override fun onTick(p0: Long) {
                invisible()
                imageView.setImageURI(Uri.parse(SettingsActivity.picArray[i]))
            }
        }
        timerBlank.start()
    }

    fun timerLoopAnswer () {
        val timerAnswer = object: CountDownTimer(5000,1000){
            override fun onFinish() {

            }

            override fun onTick(p1: Long) {
                btnX.setOnClickListener {
                    answerArray.add('X')
                }
                btnO.setOnClickListener {
                    answerArray.add('O')
                }
            }
        }
        timerAnswer.start()
    }

    fun invisible(){
        imageView.visibility = View.INVISIBLE
        btnX.visibility = View.INVISIBLE
        btnO.visibility = View.INVISIBLE
    }

    fun visible(){
        imageView.visibility = View.VISIBLE
        btnX.visibility = View.VISIBLE
        btnO.visibility = View.VISIBLE
    }

我认为由于onTick参数而跳过了较早的CountDownTimers。如何执行循环并且不跳过较早的CountDownTimers?谁能帮我吗?

0 个答案:

没有答案