Kotlin:Android Studio - 在模拟器上打印与使用 Log.v(TAG

Kotlin: Android Studio - Print on Emulator not the same as print using Log.v(TAG

fun division(){
    val numerator = 1000   
    var denominator = 4
    var index = 1
    val outputText = ("Result is ${numerator / denominator}")
    val indexText: TextView = findViewById(Rid.index_textview)
    val divisionText: TextView = findViewById(R.id.division_textView)

    repeat(3){                                  //repeats block three times
        Log.v(TAG, "${numerator / denominator}")//logs output to Logcat
        divisionText.text= outputText           //outputs the calculation text to emulator
        indexText.text = ("Index is $index")    //outputs the index value text to emulator
        Thread.sleep(1000)//milliseconds        //pauses 1 second between repeats
        denominator--                           //decrements denominator by 1
        index++                                 //increments index by 1
    }
}   

当模拟器上 Android Studio 中的代码(此处不完整)为 运行 时,我为每个 indexText 和 divisionText 得到一行文本,而不是预期的三行。此外,divisionText 的输出为 250,indexText 为 3。Android Studio logcat 正确输出每个除法计算。当我 运行 Kotlin playground 中的代码时,代码为我提供了三行 indexText 和 divisionText。但是indexText的值是正确输出的,每重复一个循环就加1,而分子/分母的计算只输出'same'每重复一行循环的最终计算。

您在 UI 线程上有一个睡眠语句,它会阻止 UI 并阻止对显示值的任何更新,直到它全部完成。使用延迟的协程而不是让数字以 1 秒的间隔正确更新,如下所示:

fun division(){
    val numerator = 1000
    var denominator = 4
    var index = 1
    val indexText: TextView = findViewById(Rid.index_textview)
    val divisionText: TextView = findViewById(R.id.division_textView)

    lifecycleScope.launch {
        repeat(3) {
            Log.v(TAG, "${numerator / denominator}")
            divisionText.text= "Result is ${numerator / denominator}"
            indexText.text = "Index is $index"
            delay(1000) //milliseconds
            denominator--
            index++
        }
    }
}   

您还需要将字符串创建移动到循环内部,以便它在每个循环中正确更新(在您的 outputText 变量保持不变之前,变量更新时不会更改)。

如果你想在不使用 lifecycleScope 的情况下执行此操作,你可以使用 MainScope() 来代替,就像这样

import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

//...

MainScope().launch {
    repeat(3) {
        // do stuff
        delay(1000)
    }
}

注意:你也使用整数除法,所以你的结果将是一个整数,而不是一个浮点数。如果你想要十进制输出,你需要先将结果转换为双精度,就像这样

val result = numerator / denominator.toDouble()
divisionText.text = "Result is $result"

或者像这样将分子更改为浮点类型

val numerator = 1000.0

这是有效的代码,感谢 Tyler!

package com.example.debugging2

import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.*

private const val TAG = "MainActivity"
class MainActivity : AppCompatActivity(){

    fun division(args: Array>String>) {
        val numerator = 1000.0
        var denominator = 4.0
        var index = 1
        val indexText: TextView = findViewById(R.id.index_textview)
        val divisionText: TextView = findViewById(R.id.division_textview)
        
        MainScope().launch {
            repeat(4)
                val result = numerator / denominator
                Log.v(TAG, "$result")
                divisionText.text = ("Division result is $result")
                indexText.text = ("Index is $index\n") //new line
                denominator--
                index++
                delay(1000)
        }
    }
}

    fun logging() {
        Log.e(TAG, "Error: a serious error like an app crash")
        Log.w(TAG, "Warn: warns about the potential for serious errors")
        Log.i(TAG, "Info: reporting technical information, such as an operation        
                    succeeding"
        Log.d(TAG, "Debug: reporting technical information useful for debugging.")
        Log.v(TAG, "Verbose: more verbose than DEBUG logs")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        logging()
        division(arrayOf())
    }
}