【问题标题】:How to call a function from within the same class activity in Kotlin? - How to loop CountDowntimer from its own class如何从 Kotlin 的同一类活动中调用函数? - 如何从自己的类中循环 CountDowntimer
【发布时间】:2019-09-15 23:13:20
【问题描述】:

我要做的是在完成后重新启动 CountdownTimer 对象。因此我把它放在一个过程'goCountdown'中,所以它会调用自己的onFinish()。然而,这会带来以下问题:

  • 不能从同一类中调用过程 goCountdown
  • 无法从 MainActivity 类之外随时间更新 B 活动文本。

参见参考文献'!!! NR !!!'在下面的代码中:

  1. 找到了 'goCountdown() 函数(已识别/不给出 错误)在位置 1,即使该过程被放置在类之外。
  2. 当将 goCountdown 放置在 nr 2 的类中时,在 nr 1 处找不到它(给出错误)。
  3. 由于它只能在类之外工作,因此现在不可能在每次滴答时更新 Activity 上的文本,因为 MainActivity 不可访问。

问题:

  • 为什么 Kotlin/Android Studio 不能识别 'goCountdown()' 函数,当它被放置在 WITHIN 中时 同一个活动课?
  • 即使这样可行,有没有办法从顶级程序访问 MainActivity 上的文本?

除了制作计时器循环的目标之外,我还试图了解它为什么不起作用。感谢您解释或指出我的解释。

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

            tapAction.setOnClickListener{
                boTimeRunning =  !boTimeRunning
                if(boTimeRunning ){
                    goCountdown() //!!! 1 !!! its fine to call it from here when its outside the class
                }
            }
            //!!! 2 !!!but if fun goCountdown() block is placed here, it is not seen at !!! 1 !!! place
        }
    }

    fun goCountdown(){
        object : CountDownTimer1(timeSettings_set * 1000, 1000){
            override fun onTick(p0: Long) {
                MainActivity.txtBig.text = "sometext" //!!! 3 !!!this doesnt work, also when MainActivity is declared as a variable object.
            }
            override fun onFinish() {
                goCountdown() //primary goal: restart the timer when its done
            }
        }.start()
    }

【问题讨论】:

  • 从我在你的代码中可以算出的,看起来你在放入 goCountdown 函数之前关闭了 MainActivity 的括号。这会导致函数不在 MainActivity

标签: android class android-studio kotlin countdowntimer


【解决方案1】:

txtBig kotlin 是综合视图还是局部变量还是全局变量?

如果 txtBig 是 kotlin 合成视图,你应该可以调用它。
如果 txtBig 不是 kotlin 合成视图,请尝试此代码。

class MainActivity : AppCompatActivity() {
    private lateinit var txtBigcopy: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        txtBig = findViewById<TextView>(R.id.txt_big) // Your id in xml
        txtBigcopy = txtBig

        tapAction.setOnClickListener{
            boTimeRunning = !boTimeRunning
            if(boTimeRunning ){
                goCountdown()
            }
        }
    }

    fun goCountdown(){
        object : CountDownTimer1(timeSettings_set * 1000, 1000){
            override fun onTick(p0: Long) {
                txtBigcopy.text = "sometext"
            }
            override fun onFinish() {
                goCountdown()
            }
        }.start()
    }
}

【讨论】:

  • 这行得通,谢谢! “private lateinit var txtBig”不能同名,所以我将其命名为 txtBigX,在 onCreate 函数中它需要说 txtBigX = txtBig。否则应用程序甚至无法运行/编译。
  • 如果回答对你有帮助,请采纳:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-06
  • 1970-01-01
相关资源
最近更新 更多