java - 在 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 字段?
解决方案
推荐阅读
- google-apps-script - Google Apps 脚本中的 Xero Api:如何刷新访问令牌?
- javascript - mongoose - 使用数组的字符串请求正文
- flutter - “MainPageDataController”不符合绑定的“StateNotifier”
' 类型参数 'Notifier' - sql - 实例非默认时如何连接本地主机 SQL Server
- c++ - 带有空元组的 BOOST_PP_TUPLE_TO_LIST
- haskell - 受约束元素类型的构造函数类/函子?
- python - 将 JSON 转换为 pandas 数据框的问题
- node.js - 发送对用户操作 Node.js 的延迟响应
- laravel - 对多个对象数组使用 laravel FormRequest prepareForValidation 方法
- python - 在Python中执行超过最大字符串长度的字符串