从Kotlin中的另一个类文件访问textview

问题描述

我在activity_receive_code.xml文件中创建一个ID为ReceivedCodeTxt的textView,然后创建一个名称SMSReceiver.kt的新kotlin文件...现在,我想在{{1}中为textView设置文本}文件,但我不知道如何访问它并更改另一类的文本。

SMSReceiver.kt

}

解决方法

您可以通过使用界面来实现

在您的SMSReceiver.kt类中创建一个接口

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
    }
}