嵌套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错误问题
发布评论