Can I databind a ProgressBar in Android?

前端 未结 4 1774
一向
一向 2021-02-13 15:37

Is it possible to use data-binding for a SeekBar or ProgressBar in Android? I have this data element:


  

        
4条回答
  •  -上瘾入骨i
    2021-02-13 16:03

    @George Mount is correct, you have to add a handler in your layout xml which is defined in your Model or Handler class (whatever you call it).

    Look at my answer for this question for a full fledged example:

    Two way databinding with Android Databinding Library

    Here's the example from that answer:

    Example:

    public class AmanteEditModel extends BaseObservable {
    
        private String senhaConfirm;
    
        @Bindable
        public String getSenhaConfirm() {
            return senhaConfirm;
        }
    
        public void setSenhaConfirm(String senhaConfirm) {
            this.senhaConfirm = senhaConfirm;
            notifyPropertyChanged(BR.senhaConfirm);
        }
    
        // Textwatcher Reference: http://developer.android.com/reference/android/text/TextWatcher.html
        public TextWatcher getMyEditTextWatcher() {
            return new TextWatcher() {
    
                public void afterTextChanged(Editable s) {
                }
    
                public void beforeTextChanged(CharSequence s, int start,
                                              int count, int after) {
                }
    
                public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                    // Important! Use the property setter, otherwhise the model won't be informed about the change.
                    setSenhaConfirm(s);
                }
            };
        }
    
    }
    

    In your layout xml change EditText to this:

    
    

    Watch for the namespace of addTextChangeListener. This method might not be available through the android: namespace, so I'm using app: here. You may also use bind: to make the binding more clear.

    So don't miss to add

    xmlns:app="http://schemas.android.com/apk/res-auto"
    

    or

    xmlns:bind="http://schemas.android.com/apk/res-auto"
    

    to your XML namespaces.

    This solution works for all input controls, custom included, given you provide the correct Listeners in your model.

    TextWatcher Reference

提交回复
热议问题