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

前端 未结 5 1803
甜味超标
甜味超标 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:51

    After digging to the bunch of methods, I found this question on SO which helped me understand how to bind single methods of listeners.

    Here is what to do with RadioGroup:

    In RadioGroup listener you have a method onCheckedChanged(RadioGroup g, int id). So you can directly bound that method to your handler or your activity by passing an instance of it as a variable in layout file and calling a method with the same signature.

    So call on layout file like this:

      
    
           ...
    
       
    

    And in my activity or handler, I need to simply provide the method with same name and signature like this:

    public void onSplitTypeChanged(RadioGroup radioGroup,int id) {
      // ...
    }
    

    Just make sure method is public.

    NOTE: This works for any (most of, I have not tried all) listener methods. Like for EditText you can provide android:onTextChanged and so on.

提交回复
热议问题