首页 > 解决方案 > 在 Kotlin 的同一个 Activity 中动态传递数据

问题描述

我有一个带有 2 个 EditText 字段的弹出布局。我在按钮点击时在我的活动中调用它

  private fun setDataInLayout() {

        val view = LayoutInflater.from(this).inflate(R.layout.popup_add_mass_and_value, null)
        val mBuilder = AlertDialog.Builder(this).setView(view)
        val mAlertDialog = mBuilder.show()
        mAlertDialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
        view.eMass?.setText(newMass) // set a string to field, but it doesn`t update it
        view.eMass?.setSelection(view.eMass.text.length!!)
        val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)

        // some further code
}

在同一个活动中,我有一个蓝牙比例代码,我从中得到了质量

 private val task: Runnable = object : Runnable {
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
        override fun run() {
            handler.postDelayed(this, 150)
            if (!scale!!.bleIsEnabled()) {
                return
            }
          if(condition){
           //here I need to set weight from scale to eMass
            newMass=java.lang.String.format("%4.1f",
                scale!!.getDevicelist()!![i].scalevalue)   
           //here I tried to set my string variable                       
           }
}}

我尝试了两种方法,第一种是直接访问 Runnable 中的 eMass 字段,但我不知道如何正确访问它。第二种方式,我声明了一个字符串 newMass,我在 Runable 中正确设置了它,但我无法将它传递给 eMass 字段,只有当我关闭弹出窗口并再次打开它时才会显示值。我究竟做错了什么?是否有另一种方法可以将质量从我的比例传递到弹出布局内的 EditText 字段?

标签: javaandroidandroid-layoutkotlin

解决方案


推荐阅读