从 kotlin 中的另一个 class 文件访问文本视图

Access a textview from another class file in kotlin

我在 activity_receive_code.xml 文件中创建了一个带有 ID ReceivedCodeTxt 的 textView,然后我创建了一个名为 SMSReceiver.kt 的新 kotlin 文件...现在我想在我的 [=] 中为一个 textView 设置文本13=] 文件,但我不知道如何访问它并更改另一个 class.

的文本
    val bundle = intent!!.extras
    try {
        if (bundle != null) {
            val pdusObj = bundle["pdus"] as Array<*>?
            for (i in pdusObj!!.indices) {
                val currentMessage = SmsMessage.createFromPdu(pdusObj[i] as ByteArray)
                val phoneNumber = currentMessage.displayOriginatingAddress
                val message = currentMessage.displayMessageBody
                Log.i("SmsReceiver", "senderNum: $phoneNumber; message: $message")
                // Show alert
                val toast = Toast.makeText(context,"senderNum: $phoneNumber, message: $message",Toast.LENGTH_LONG).show()

            } // end for loop
        } // bundle is null
    } catch (e: Exception) {
        Log.e("SmsReceiver", "Exception smsReceiver$e")
    }


}

}

您可以使用界面来完成此操作

在您的 SMSReceiver.kt class

中创建一个界面
class SMSReceiver.kt(val callback:ICallback) {
    interface ICallback{
        fun updateUI(value:String)
    }

    fun doSomething() {
       // your operations to perform some task. then call this
       callback.updateUI(/*Pass your data which you want to set to your textview*/ "")
    }
}


class ActivityReceive: SMSReciever.ICallback {

    override fun updateUI(value:String) {
        //set the data which you got from SMSReceiver class
        textview.text = value
    }
}