【发布时间】:2021-04-19 04:18:33
【问题描述】:
当我关闭对话框时,我需要获取选定的选项名称(文本)而不是位置,但它一直返回项目位置。
Code
pType.setOnClickListener{
val singleItems = arrayOf("Item 1", "Item 2", "Item 3")
val checkedItem = 0
pType.text = null
MaterialAlertDialogBuilder(this)
.setTitle(resources.getString(R.string.project_type))
.setNeutralButton(resources.getString(R.string.cancel)) { dialog, which ->
// Respond to neutral button press
}
.setPositiveButton(resources.getString(R.string.ok)) { dialog, which ->
// Respond to positive button press
}
// Single-choice items (initialized with checked item)
.setSingleChoiceItems(singleItems, checkedItem) { dialog, which ->
// Respond to item chosen
pType.setText(checkedItem.toString())
}
.show()
}
Questions
- 在对话框中单击“确定”时如何获取选项文本?
- 通过使用
val checkedItem = 0,无论我选择什么选项,总是选择option 0,我该如何避免这种情况?
【问题讨论】:
-
regardless of my selected option always option 0 is selected除了第一次将其设为零之外,您永远不会为其分配其他值,因此它应该这样做。 -
@a_local_nobody 感谢您没有展示如何分配其他值而不是 0!
it's fixed now -
没有必要对它粗鲁。我只是说,如果你有一个声明为
val的东西,它就不能改变,这就是val的意义所在。如果你想改变它,那么它可能应该是一个 var,或者你应该在需要时从某个地方读取它
标签: android kotlin material-design