问题出现原因是AlertDialog的View无法正确地更新RangeSlider的值。为解决此问题,我们可以使用自定义布局来实现AlertDialog中的RangeSlider,然后在自定义布局中使用RangeSlider的监听器来更新值。以下是示例代码:
首先,在你的布局文件中添加RangeSlider:
然后,在你的Activity中,使用自定义布局来创建AlertDialog:
val alertDialogView = LayoutInflater.from(this).inflate(R.layout.alert_dialog_layout, null)
val rangeSlider = alertDialogView.findViewById(R.id.range_slider)
val builder = AlertDialog.Builder(this)
builder.setView(alertDialogView)
builder.setPositiveButton("OK") { dialog, which ->
// handle OK button click here
}
builder.setNegativeButton("Cancel") { dialog, which ->
// handle Cancel button click here
}
val dialog = builder.create()
rangeSlider.addOnChangeListener { slider, value, fromUser ->
// update values here
}
dialog.show()
通过这种方式,你就可以在AlertDialog中正确地更新RangeSlider的值了。