java Tow-way数据绑定字符串,基于Fabio Colini的Blog

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Tow-way数据绑定字符串,基于Fabio Colini的Blog相关的知识,希望对你有一定的参考价值。

<?xml version="1.0" encoding="utf-8"?>
<!-- Put this in /res/values -->
<resources>
    <item name="textBound" type="id" />
</resources>
import android.databinding.BaseObservable;
import android.databinding.BindingAdapter;
import android.databinding.BindingConversion;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;


import java.io.Serializable;

/**
 * An observable class that holds a String, including event listener logic that will
 * set data back to the observable upon change in the UI layer.
 *
 * Credits to Fabio Collini https://medium.com/@fabioCollini/android-data-binding-f9f9d3afc761#.7xkobedbt
 */
public class TwoWayBoundString extends BaseObservable implements Serializable {
    static final long serialVersionUID = 1L;
    private String mValue;

    /**
     * Creates an empty observable object
     */
    public TwoWayBoundString() {
    }

    /**
     * Wraps the given object and creates an observable object
     *
     * @param value The value to be wrapped as an observable.
     */
    public TwoWayBoundString(String value) {
        mValue = value;
    }

    /**
     * @return the stored value.
     */
    public String get() {
        return mValue;
    }

    /**
     * Set the stored value.
     */
    public void set(String value) {
        if (value == null && mValue == null) return;
        if ((value == null && mValue != null) || !value.equals(mValue)) {
            mValue = value;
            notifyChange();
        }
    }

    /** Sets the stored value without notifying of change */
    public void setSilently(String value) {
        mValue = value;
    }

    @BindingAdapter({"android:text"})
    public static void bindEditText(EditText view, final TwoWayBoundString twoWayBoundString) {
        if (view.getTag(R.id.textBound) == null) {
            // Hook up chanbge listeners upon first initialization
            view.setTag(R.id.textBound, true);
            view.addTextChangedListener(new TextWatcher() {
                @Override
                public void afterTextChanged(Editable s) {
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    twoWayBoundString.set(s.toString());
                }
            });
        }
        String newValue = twoWayBoundString.get();
        if (!view.getText().toString().equals(newValue)) {
            view.setText(newValue);
        }
    }

    // Fallback one-way binding for non-text String attributes, like "android:hint"
    @BindingConversion
    public static String observableStringToString(TwoWayBoundString string) {
        return string.get();
    }
}

以上是关于java Tow-way数据绑定字符串,基于Fabio Colini的Blog的主要内容,如果未能解决你的问题,请参考以下文章

java 滚动感知FloatingActionBar - FAB

出现屏幕键盘时隐藏 FAB

如何在 FAB 按钮单击时将数据从多个片段发送到单个活动

AndroidStudio编程,如何把圆形ProgressBar的中心位置和FloatingActionButton的中心位置进行绑定?

不使用 web.xml 时如何将 JDBC 数据源绑定到 JNDI 上下文“java:comp/env/jdbc”

您可以在 XAML 属性数据绑定期间对超链接的 NavigateUri 属性进行 StringFormat 吗?