我在Android的Kotlin中有这段代码,我需要将append应用于messageReceived变量。我该怎么做?

private lateinit var messageReceived: TextView

this.messageReceived = findViewById(R.id.messageRreceived)

private fun updateReceivedMessage(newPayload: String) {
    runOnUiThread { messageReceived.text = newPayload }
}

最佳答案

首先获取文本,然后设置新文本

private fun updateReceivedMessage(newPayload: String) {
    runOnUiThread {
        val previousText = messageReceived.text.toString()
        messageReceived.text = "$previousText $newPayload"
    }
}
您可以通过换行使其更具可读性
messageReceived.text = "$previousText\n$newPayload"

10-04 19:23