Android实现RadioGroup之间的互斥且radioButton可以选择或取消

Posted Tobey_r1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android实现RadioGroup之间的互斥且radioButton可以选择或取消相关的知识,希望对你有一定的参考价值。

android实现RadioGroup之间的互斥且radioButton可以选择或取消

关于

  我前段时间写了一篇《Android实现RadioGroup之间的互斥》,本以为功能需求到此结束了,没想到,现在要求两个radiogroup互斥,并且每个radioButton都可以选择和取消,好吧,所以有这第二篇这么一说了。

效果图

主界面

  布局可以去《Android实现RadioGroup之间的互斥》去看一下,这里直接贴修改后的页面代码了:

class RadioGroupActivity : AppCompatActivity() 

    private lateinit var viewBinding: ActivityRadioGroupBinding

    private var faceFeet = ""
    private var feet = ""
    private var faceBalance = ""
    private var window =""
    private var face = ""
    private var faceWindow = ""

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        viewBinding = ActivityRadioGroupBinding.inflate(layoutInflater)
        setContentView(viewBinding.root)
        initView()
        initRadioButton()
    
    

    private fun initView() 
        viewBinding.tvSwitch2.setOnClickListener 
            setRadioButtonStatus(it,"faceWindow")
        
        viewBinding.tvSwitch1.setOnClickListener 
            setRadioButtonStatus(it,"face")
        
        viewBinding.tvSwitch3.setOnClickListener 
            setRadioButtonStatus(it,"faceBalance")
        
        viewBinding.tvSwitch4.setOnClickListener 
            setRadioButtonStatus(it,"feet")
        
        viewBinding.tvSwitch5.setOnClickListener 
            setRadioButtonStatus(it,"faceFeet")
        
        viewBinding.tvSwitch6.setOnClickListener 
            setRadioButtonStatus(it,"window")
        
    

    private fun setRadioButtonStatus(view: View, value: String) 
        when(view.id)
            R.id.tvSwitch6 ->
                if (value == window) 
                    viewBinding.rgFlowTwoSwitch.check(-1)
                    viewBinding.rgFlowSwitch.clearCheck()
                    window = ""
                 else 
                    window = value
                    feet = ""
                    faceFeet =""
                    faceBalance =""
                    face = ""
                    faceWindow = ""
                    viewBinding.rgFlowTwoSwitch.check(view.id)
                    viewBinding.rgFlowSwitch.clearCheck()

                
            
            R.id.tvSwitch5 ->
                if (value == faceFeet) 
                    viewBinding.rgFlowTwoSwitch.check(-1)
                    viewBinding.rgFlowSwitch.clearCheck()
                    faceFeet = ""
                 else 
                    faceFeet = value
                    feet = ""
                    window = ""
                    faceBalance = ""
                    face = ""
                    faceWindow = ""
                    viewBinding.rgFlowTwoSwitch.check(view.id)
                    viewBinding.rgFlowSwitch.clearCheck()
                
            
            R.id.tvSwitch4 ->
                if (value == feet) 
                    viewBinding.rgFlowTwoSwitch.check(-1)
                    viewBinding.rgFlowSwitch.clearCheck()
                    feet = ""
                 else 
                    feet = value
                    faceFeet = ""
                    window = ""
                    faceBalance = ""
                    face = ""
                    faceWindow = ""
                    viewBinding.rgFlowTwoSwitch.check(view.id)
                    viewBinding.rgFlowSwitch.clearCheck()
                
            
            R.id.tvSwitch3 ->
                if (value == faceBalance) 
                    viewBinding.rgFlowSwitch.check(-1)
                    viewBinding.rgFlowTwoSwitch.clearCheck()
                    faceBalance = ""
                 else 
                    faceBalance = value
                    faceFeet = ""
                    feet = ""
                    face = ""
                    window = ""
                    faceWindow = ""
                    viewBinding.rgFlowSwitch.check(view.id)
                    viewBinding.rgFlowTwoSwitch.clearCheck()
                
            
            R.id.tvSwitch2 ->
                if (value == faceWindow) 
                    viewBinding.rgFlowSwitch.check(-1)
                    viewBinding.rgFlowTwoSwitch.clearCheck()
                    faceWindow = ""
                 else 
                    faceWindow = value
                    face = ""
                    faceFeet = ""
                    feet = ""
                    window = ""
                    faceBalance = ""
                    viewBinding.rgFlowSwitch.check(view.id)
                    viewBinding.rgFlowTwoSwitch.clearCheck()
                
            
            R.id.tvSwitch1 ->
                if (value == face) 
                    viewBinding.rgFlowSwitch.check(-1)
                    viewBinding.rgFlowTwoSwitch.clearCheck()
                    face = ""
                 else 
                    face = value
                    faceFeet = ""
                    faceWindow = ""
                    feet = ""
                    window = ""
                    faceBalance = ""
                    viewBinding.rgFlowSwitch.check(view.id)
                    viewBinding.rgFlowTwoSwitch.clearCheck()
                
            
        
    

    //初始化给某一个radioButton选中
    private fun initRadioButton()
        val currentSelect = val currentSelect = "tvSwitch$2"
        val currentId =resources.getIdentifier(currentSelect,"id",packageName)
        findViewById<RadioButton>(currentId)?.isChecked = true
        when(currentId)
            R.id.tvSwitch1 -> face = "face"
            R.id.tvSwitch2 -> faceWindow = "faceWindow"
            R.id.tvSwitch3 -> faceBalance = "faceBalance"
            R.id.tvSwitch4 -> feet = "feet"
            R.id.tvSwitch5 -> faceFeet = "faceFeet"
            R.id.tvSwitch6 -> window = "window"
        
    

  文章到此结束了,有问题欢迎批评指教,觉得不错的也请点个赞谢谢。

以上是关于Android实现RadioGroup之间的互斥且radioButton可以选择或取消的主要内容,如果未能解决你的问题,请参考以下文章

android以编程方式在radiogroup按钮之间添加填充

android如何实现代码控制RadioGroup中某一个按钮选中

android如何实现代码控制RadioGroup中某一个按钮选中

android 中RadioGroup怎么实现横排按钮

Android RadioGroup和RadioButton详解

Android 中使用RadioGroup+Fragment实现底部导航栏的功能