How to bind method on RadioGroup on checkChanged event with data-binding

前端 未结 5 1712
甜味超标
甜味超标 2021-01-31 09:32

I was searching over the internet for how to perform the new cool android data-binding over the RadioGroup and I didn\'t find a single blog post about it.

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-31 09:45

    After some hours I found easy way: two-way databinding in android. It's base skeleton with livedata and Kotlin. Also you can use ObservableField()

    1. Set your viewmodel to data
    2. Create your radiogroup with buttons as you like. Important: set all radio buttons id !!!
    3. Set in your radio group two-way binding to checked variable (use viewmodel variable)
    4. Enjoy)

    layout.xml

    
        
    
    
    
    
    
    
                
    
                    
    
                    
    
                    
    
                    
                
    
            
    

    class YourViewModel(): ViewModel {
    
    var radio_checked = MutableLiveData()
    
    
    init{
        radio_checked.postValue(R.id.your_id1)//def value
    }
    
    //other code
    }
    

提交回复
热议问题