无法使用 Android 数据绑定绑定 double

Cannot bind double with Android databinding

我有一个扩展 BaseObservable 的 class,这个 class 包含双值

这是一个例子:

public class BindedValue extends BaseObservable {

public double value;


public TextWatcher setValue = new TextWatcherAdapter(){

    @Override
    public void afterTextChanged(Editable s) {
        value = Double.parseDouble(s.toString());
    }
  };
}

然后我得到了xml

<data class="net.example.util.Value">

            <variable
        name="BindedValue"
        type="net.makereal.makerealmaquette.domain.BindedValue"/>

    <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:hint="Value"
                android:addTextChangedListener="@{BindedValue.setValue}"
                android:inputType="numberDecimal"
                android:text="@{BindedValue.value}"/>

当我尝试 运行 或构建应用程序时出现此错误:

Cannot find the setter for attribute 'android:text' with parameter type double on android.widget.EditText.

然而,当我将类型更改为 int 时,应用程序构建没有问题。

有没有绑定double的方法?

However when I change the type to int the app builds with now issue.

是的,但它会在运行时失败。 setText(int) 需要一个字符串资源 ID,而不是任意 int.

Is there way to bind double?

android:text="@{Double.toString(BindedValue.value)}"

这是将对象转换为 String:

的另一种选择
android:text="@{String.valueOf(BindedValue.value)}"

此语法也适用:

android:text='@={""+item.value}'

并且可以通过添加一个String setter:

来反转
public void setValue(String value) {
    this.setValue(Double.valueOf(value));
}