我如何在kotlin中使用非静态方法?

时间:2018-05-08 12:39:25

标签: java android kotlin static-methods

我有一个扩展BroadcastReceiver的类。因此,当消息到达时,该方法被调用。现在在该方法中,我正在调用另一个类的方法来设置EditText中的文本。但由于我使用kotlin,我需要在伴侣对象中使用静态方法。那么如何在该方法或其他地方为我的setText调用EditText

我的BroadcastReceiver班级:

class SMS : BroadcastReceiver() {

    val OTP_REGEX = "[0-9]{1,6}"

    override fun onReceive(context: Context, intent: Intent) {
        Log.e("onReceive", "==->$intent")

        val bundle = intent.extras
        try {
            if (bundle != null) {
                Log.e("onReceive", "==->$bundle")
                val pdusObj = bundle.get("pdus") as Array<Any>
                for (i in pdusObj.indices) {
                    val currentMessage = SmsMessage.createFromPdu(pdusObj[i] as ByteArray)
                    val phoneNumber = currentMessage.displayOriginatingAddress
                    val senderNum = phoneNumber
                    val message = currentMessage.displayMessageBody

                    Log.e("getMessage", "===->$message")

                    try {
                        VerifyOTPActivity.ReceievdMsg(message);
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

} 

我需要在编辑文本中设置该消息的类。

class VerifyOTPActivity : AppCompatActivity()  {

    companion object {

        @JvmStatic
        fun ReceievdMsg(message: String) {
            var otp = ""
            val OTP_NUMBERS = "[0-9]{1,6}"
            val pattern = Pattern.compile(OTP_NUMBERS)
            val matcher = pattern.matcher(message)

            while (matcher.find()) {
                otp = matcher.group()
            }

            try {
               // edt.setText(otp) // Edit text
            } catch (e: Exception) {

            }
        }

    }

    var mobileNo: String = ""

    var preference: AppPreference? = null
    lateinit var adminAPI: AdminAPI
    lateinit var customDialog: CustomDialog

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_verify_otp)

        preference = Utils.getAppPreference(applicationContext)
        adminAPI = ServiceGenerator.getAPIClass()
        customDialog = CustomDialog(this)
        customDialog.setCancelable(false)

        val intent: Intent = intent

        mobileNo = intent.getStringExtra("mobileNo")

        workonIds()
        setupToolBar()
    }

    private fun setupToolBar() {
        txt_toolbar_title.gravity = Gravity.CENTER
    }

    private fun workonIds() {
        txt_mobile_no.setText(mobileNo)

        txt_mobile_no.setOnClickListener{
            onBackPressed()
        }

        layout_next.setOnClickListener {
            val getOTP = edt_otp.text.toString().trim()
            val getMobileNo = txt_mobile_no.text.toString().trim()

            if (getOTP.isEmpty()) {
                C.showToast(applicationContext, resources.getString(R.string.emptyotp))
            } else if (getOTP.length < 6) {
                C.showToast(applicationContext, resources.getString(R.string.validotp))
            } else {
                verifyOTP(getMobileNo , getOTP)
            }
        }
    }

    private fun verifyOTP(mobileNo: String, otp: String) {
        customDialog.show()
        val sendOTP : Call<AppModel> = adminAPI.VerifyOTP(mobileNo , otp)
        sendOTP.enqueue(object : Callback<AppModel> {
            override fun onFailure(call: Call<AppModel>?, t: Throwable?) {
                customDialog.dismiss()
                C.errorToast(applicationContext , t)
            }

            override fun onResponse(call: Call<AppModel>?, response: Response<AppModel>?) {
                customDialog.dismiss()
                val appModel : AppModel = response!!.body()!!
                if (appModel != null) {
                    if (appModel.isStatus) {
                        val intent = Intent(applicationContext, UserRegistrationActivity::class.java)
                        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                        preference!!.verify = true
                        preference!!.mobileNumber = mobileNo
                        intent.putExtra("mobileNo", mobileNo)
                        startActivity(intent)
                    } else {
                        C.showToast(applicationContext , appModel.message)
                    }
                } else {
                    C.defaultError(applicationContext)
                }
            }
        })
    }

}

如何在EditText

中设置该讯息

2 个答案:

答案 0 :(得分:1)

您可以在VerifyOTPActivity中创建内部类,并使用BroadcastReceiver进行扩展。在您的onReceive()内,您可以访问VerifyOTPActivity个实例,并可以致电findViewById()让您的TextView实例在那里写下您的文字。 当然,您应该在registerReceiver()的{​​{1}}内onCreate()VerifyOTPActivity内的unregisterReceiver()

换句话说,只需将onDestroy() braodcast接收器设为SMS,并将其注册绑定到活动生命周期

答案 1 :(得分:1)

您可以设置一个单例的事件总线。通过BroadcastReceiver您可以将活动发送到公交车以及Activity / Fragment / Whatever,您可以订阅这些活动并作出相应的反应

虽然我强烈推荐EventBus,但您可以查看OttoRxJava等库。它有点复杂但方式更强大