Android DataBinding float to TextView



我正在尝试绑定:

 @Bindable
public float getRoundInEditAmount()
{
    return roundInEdit.getAmount();
}
@Bindable
public void setRoundInEditAmount(float amount)
{
    roundInEdit.setAmount(amount);
    notifyPropertyChanged(BR.roundInEditAmount);
}

to

 <EditText
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:inputType="numberDecimal"
            android:text="@={`` + weightSet.roundInEditAmount}"
            ></EditText>

但是,在单击EditText时,我会向我提供一个文本输入而不是数字板。如果我再次单击此EDITTEXT,则会向我提供数字垫。如果该字段默认为50.0或其他值,我将无法删除这些金额。我可以输入文本,并且确实可以持续。

是否有人遇到过这种行为,文本输入首次单击而不是数字板?还按照我期望的方式对iTittext进行两种绑定。我已经编写了自己的绑定和逆框适配器,它们以相同的方式行为 ->在第一次单击时进行textInput,然后在第二次单击时进行编号pad,但您无法删除以。

尝试像这样

<EditText
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:inputType="numberDecimal"
            android:text="@={String.valueOf(weightSet.roundInEditAmount)}"/>

如果使用Android数据列表库,它可以通过创建绑定适配器来解决。

public class BindingUtils {
    @BindingAdapter("android:text")
    public static void setFloat(TextView view, float value) {
        if (Float.isNaN(value)) view.setText("");
        else view.setText( ... you custom formatting );
    }
    @InverseBindingAdapter(attribute = "android:text")
    public static float getFloat(TextView view) {
        String num = view.getText().toString();
        if(num.isEmpty()) return 0.0F;
        try {
           return Float.parseFloat(num);
        } catch (NumberFormatException e) {
           return 0.0F;
        }
    }
}

最新更新