How to perform two-way data binding with a ToggleButton?

前端 未结 4 1491
花落未央
花落未央 2020-12-29 22:41

I have an ObservableBoolean field in my activity class, which is bound to the \"checked\" attribute of my ToggleButton like so:

and         


        
4条回答
  •  盖世英雄少女心
    2020-12-29 23:23

    You need another '=' to tell Android that you want to use Two-Way databinding:

    android:checked="@={activity.editing}"
    

    You can find more information on this in the wordpress article of George Mount:

    Two-Way Data Binding

    Android isn’t immune to typical data entry and it is often important to reflect changes from the user’s input back into the model. For example, if the above data were in a contact form, it would be nice to have the edited text pushed back into the model without having to pull the data from the EditText. Here’s how you do it:

    
        
            
        
        
            
        
    
    

    Pretty nifty, eh? The only difference here is that the expression is marked with @={} instead of @{}. It is expected that most data binding will continue to be one-way and we don’t want to have all those listeners created and watching for changes that will never happen.

提交回复
热议问题