当我调用这个 radiogroupId.clearCheck() 这个方法时调用 radiogroup click listener
Whne i call this radiogroupId.clearCheck() this method recall radiogroup click listener
详细信息:我有 2 个带有一些单选按钮的单选组。
我想要的: 仅限用户 select 2 组中的一个单选按钮。
我做了什么:
rgOne.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->
try {
val checkedRadioButtonId: Int = rgOne.checkedRadioButtonId
if (checkedRadioButtonId != null) {
oneItem = oneList[checkedRadioButtonId]
healthy = oneItem.code!!
AppGlobal.displayShortToast(
this, "You selected: " + oneItem.title
)
if (rgTwo.checkedRadioButtonId != -1) {
rgTwo.clearCheck()
}
}
} catch (e: Exception) {
e.printStackTrace()
}
})
rgTwo.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->
try {
val checkedRadioButtonId: Int = rgTwo.checkedRadioButtonId
if (checkedRadioButtonId != null) {
twoItem = twoList[checkedRadioButtonId]
unhealthy = twoItem.code!!
AppGlobal.displayShortToast(
this, "You selected: " + twoItem.title
)
if (rgOne.checkedRadioButtonId != -1) {
rgOne.clearCheck()
}
}
} catch (e: Exception) {
e.printStackTrace()
}
})
面对问题: 当我调用这个 rgOne.clearCheck() 或 rgTwo.clearCheck() 时,调用整个广播组点击监听器并取消选中我检查。第一次工作完美,就像什么都没有 selected.
这段代码有什么问题以及我需要更改什么。
在 Activity 的 onCreate() 或 Fragment 的 onCreateView() 中调用此行一次:
rgOne.setOnCheckedChangeListener(onCheckedChangeListener)
rgTwo.setOnCheckedChangeListener(onCheckedChangeListener)
声明这个变量:
private val onCheckedChangeListener =
RadioGroup.OnCheckedChangeListener { group, checkedId ->
if (group!!.id == R.id.rgOne) {
try {
AppLog.e("Call healthy")
val checkedRadioButtonId: Int = rgOne.checkedRadioButtonId
if (checkedRadioButtonId != null) {
if (rgTwo.checkedRadioButtonId != -1) {
resetRadioGroup(rgTwo)
}
}
} catch (e: Exception) {
e.printStackTrace()
}
} else {
try {
AppLog.e("Call Unhealthy")
val checkedRadioButtonId: Int = rgTwo.checkedRadioButtonId
if (checkedRadioButtonId != null) {
if (rgOne.checkedRadioButtonId != -1) {
resetRadioGroup(rgOne)
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
重置广播组的功能:
fun resetRadioGroup(rgType: RadioGroup) {
rgType.setOnCheckedChangeListener(null)
rgType.clearCheck()
rgType.setOnCheckedChangeListener(onCheckedChangeListener)
}
详细信息:我有 2 个带有一些单选按钮的单选组。
我想要的: 仅限用户 select 2 组中的一个单选按钮。
我做了什么:
rgOne.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->
try {
val checkedRadioButtonId: Int = rgOne.checkedRadioButtonId
if (checkedRadioButtonId != null) {
oneItem = oneList[checkedRadioButtonId]
healthy = oneItem.code!!
AppGlobal.displayShortToast(
this, "You selected: " + oneItem.title
)
if (rgTwo.checkedRadioButtonId != -1) {
rgTwo.clearCheck()
}
}
} catch (e: Exception) {
e.printStackTrace()
}
})
rgTwo.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->
try {
val checkedRadioButtonId: Int = rgTwo.checkedRadioButtonId
if (checkedRadioButtonId != null) {
twoItem = twoList[checkedRadioButtonId]
unhealthy = twoItem.code!!
AppGlobal.displayShortToast(
this, "You selected: " + twoItem.title
)
if (rgOne.checkedRadioButtonId != -1) {
rgOne.clearCheck()
}
}
} catch (e: Exception) {
e.printStackTrace()
}
})
面对问题: 当我调用这个 rgOne.clearCheck() 或 rgTwo.clearCheck() 时,调用整个广播组点击监听器并取消选中我检查。第一次工作完美,就像什么都没有 selected.
这段代码有什么问题以及我需要更改什么。
在 Activity 的 onCreate() 或 Fragment 的 onCreateView() 中调用此行一次:
rgOne.setOnCheckedChangeListener(onCheckedChangeListener)
rgTwo.setOnCheckedChangeListener(onCheckedChangeListener)
声明这个变量:
private val onCheckedChangeListener =
RadioGroup.OnCheckedChangeListener { group, checkedId ->
if (group!!.id == R.id.rgOne) {
try {
AppLog.e("Call healthy")
val checkedRadioButtonId: Int = rgOne.checkedRadioButtonId
if (checkedRadioButtonId != null) {
if (rgTwo.checkedRadioButtonId != -1) {
resetRadioGroup(rgTwo)
}
}
} catch (e: Exception) {
e.printStackTrace()
}
} else {
try {
AppLog.e("Call Unhealthy")
val checkedRadioButtonId: Int = rgTwo.checkedRadioButtonId
if (checkedRadioButtonId != null) {
if (rgOne.checkedRadioButtonId != -1) {
resetRadioGroup(rgOne)
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
重置广播组的功能:
fun resetRadioGroup(rgType: RadioGroup) {
rgType.setOnCheckedChangeListener(null)
rgType.clearCheck()
rgType.setOnCheckedChangeListener(onCheckedChangeListener)
}