关于radiogroup嵌套radiobutton并设置监听回调时radiogroup getcheckedId错误问题

编程入门 行业动态 更新时间:2024-10-09 14:23:17

关于radiogroup<a href=https://www.elefans.com/category/jswz/34/1771299.html style=嵌套radiobutton并设置监听回调时radiogroup getcheckedId错误问题"/>

关于radiogroup嵌套radiobutton并设置监听回调时radiogroup getcheckedId错误问题

使用的kotlin
代码如下

for (index in 0  until rg1.childCount){val rb = rg1.getChildAt(index) as RadioButtonrb.setOnCheckedChangeListener { _, b ->if (b){btn1.text = "¥${getPrice(rg1,rg2)}支付宝"btn2.text = "¥${getPrice(rg1,rg2)}微信"}}}

调用checkedId代码

private fun getPrice(rg1 : RadioGroup,rg2 : RadioGroup) : Int = when (rg1.checkedRadioButtonId){R.id.rb1 -> when(rg2.checkedRadioButtonId){R.id.rb11 -> (banner.adapter.getData(1) as Vip).monthPrice.toInt()R.id.rb22 -> (banner.adapter.getData(1) as Vip).yearPrice.toInt()else -> 0}R.id.rb2 -> when(rg2.checkedRadioButtonId){R.id.rb11 -> (banner.adapter.getData(2) as Vip).monthPrice.toInt()R.id.rb22 -> (banner.adapter.getData(2) as Vip).yearPrice.toInt()else -> 0}R.id.rb3 -> when(rg2.checkedRadioButtonId){R.id.rb11 -> (banner.adapter.getData(3) as Vip).monthPrice.toInt()R.id.rb22 -> (banner.adapter.getData(3) as Vip).yearPrice.toInt()else -> 0}else -> 0}

这样调用每次checkedid却是上一次的id
打断点看了下,当点击时回调,获取到的radiogroup的checkedid同时有两个
太坑人了
解决办法
加一个1ms的延迟即可解决

for (index in 0  until rg1.childCount){val rb = rg1.getChildAt(index) as RadioButtonrb.setOnCheckedChangeListener { _, b ->if (b){Handler().postDelayed({btn1.text = "¥${getPrice(rg1,rg2)}支付宝"btn2.text = "¥${getPrice(rg1,rg2)}微信"},1)}}}

不知道算不算bug…

更多推荐

关于radiogroup嵌套radiobutton并设置监听回调时radiogroup getcheckedId错误问题

本文发布于:2024-02-13 04:53:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1690759.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   回调   错误   radiogroup   getcheckedId

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!