如何从 Kotlin 中的同一个 class activity 中调用一个函数? - 如何从它自己的循环 CountDowntimer class
How to call a function from within the same class activity in Kotlin? - How to loop CountDowntimer from its own class
我想做的是在 CountdownTimer 对象完成后重新启动它。因此我把它放在一个过程 'goCountdown' 中,所以它会调用自己的 onFinish() 。然而,这会带来以下问题:
- A procedure goCountdown cannot be called from within the same class
- B activity text cannot be updated with time from outside the MainActivity class.
查看参考资料'!!!不!在下面的代码中:
- 找到 'goCountdown() 函数(recognized/doesnt 给出一个
错误)在位置 1,即使该过程位于 class.
之外
- 当 goCountdown 被放置在 nr 2 的 class 内时,它在 nr 1 的位置找不到(给出错误)。
- 因为它只能在 class 之外工作,所以现在不可能在每次更新时更新 activity 上的文本,因为 MainActivity 不可访问。
问题:
- 为什么 Kotlin / Android Studio 无法识别放在 WITHIN 中的 'goCountdown()' 函数
同样的activityclass?
- 即使这行得通,有没有办法从顶级过程访问 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()
}
txtBig kotlin合成视图是局部变量还是全局变量?
如果txtBig是kotlin合成视图,应该可以调用。
如果 txtBig 不是 kotlin synthetic veiw,请尝试此代码。
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()
}
}
我想做的是在 CountdownTimer 对象完成后重新启动它。因此我把它放在一个过程 'goCountdown' 中,所以它会调用自己的 onFinish() 。然而,这会带来以下问题:
- A procedure goCountdown cannot be called from within the same class
- B activity text cannot be updated with time from outside the MainActivity class.
查看参考资料'!!!不!在下面的代码中:
- 找到 'goCountdown() 函数(recognized/doesnt 给出一个 错误)在位置 1,即使该过程位于 class. 之外
- 当 goCountdown 被放置在 nr 2 的 class 内时,它在 nr 1 的位置找不到(给出错误)。
- 因为它只能在 class 之外工作,所以现在不可能在每次更新时更新 activity 上的文本,因为 MainActivity 不可访问。
问题:
- 为什么 Kotlin / Android Studio 无法识别放在 WITHIN 中的 'goCountdown()' 函数 同样的activityclass?
- 即使这行得通,有没有办法从顶级过程访问 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()
}
txtBig kotlin合成视图是局部变量还是全局变量?
如果txtBig是kotlin合成视图,应该可以调用。
如果 txtBig 不是 kotlin synthetic veiw,请尝试此代码。
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()
}
}