我正在尝试使用其他选项来保存 Dialogfragment 中的值。一旦保存,但如果我重新打开对话框 fragment 屏幕值更改为“无应答”..它不会保持保存状态或调用我上次保存的值。 当我选择值并保存时我的第一张图像
当我重新打开对话框时
对话框
class HeightDialog : DialogFragment() {
interface SingleChoiceListner {
fun onPositiveButtonClicked(list: Array<String>?, positon: Int)
fun onNegativeButtonClicked()
}
internal var positition = 0
lateinit var mSingleChoiceListner: SingleChoiceListner
override fun onAttach(context: Context) {
super.onAttach(context)
try {
mSingleChoiceListner = context as SingleChoiceListner
} catch (e: Exception) {
throw ClassCastException(activity.toString() + "SingleChoiceListner must be implemented")
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(activity)
val heightArray = activity?.resources?.getStringArray(R.array.height_array)
builder.setTitle("Select Your Height")
.setSingleChoiceItems(
heightArray,
positition
) { dialog, which -> positition=which }
.setPositiveButton(
"ok"
) { dialog, which ->
mSingleChoiceListner.onPositiveButtonClicked(
heightArray,
positition
)
}
.setNegativeButton(
"cancle"
) { dialog, which -> mSingleChoiceListner.onNegativeButtonClicked() }
return builder.create()
}
}
Activity
override fun onPositiveButtonClicked(list: Array<String>?, positon: Int) {
val height = list?.get(positon)
heightPosition=positon
newHeight = "${height?.replace("cm", "")}"
height_tv.text = height
}
override fun onNegativeButtonClicked() {
}
最佳答案
setSingleChoiceItems
中的第二个参数负责对话框中的默认值。
如果您想显示此对话框中最后选择的项目,您应该保存此位置的某个位置,稍后当您调用此对话框时检索您的值并设置它。
关于java - 所选值保留保存在带有单选按钮 Android 的 Dialogfragment 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60113224/