Android DataBinding浮动到TextView [英] Android DataBinding float to TextView

查看:131
本文介绍了Android DataBinding浮动到TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绑定:

 @Bindable
public float getRoundInEditAmount()
{
    return roundInEdit.getAmount();
}

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

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

但是,在单击EditText时,会显示一个文本输入而不是数字键盘.如果再次单击此EditText,则会显示数字键盘.如果该字段默认为50.0或其他值,则无法删除这些金额.虽然我可以输入文字,但它确实会持续存在.

However on clicking the EditText I am presented with a a text input not the number pad. If I click this EditText again I am then presented with the number pad. If the field has been defaulted to 50.0 or another value I cannot delete these amounts. I can enter text though and it does persist.

是否还有其他人遇到这种行为,其文本输入是在第一次单击时出现的,而不是数字键盘上的?在EditText上的两种方式绑定也按我期望的方式工作.我已经编写了自己的Binding和InverseBinding适配器,它们的行为方式相同->第一次单击时为TextInput,然后在第二次单击时为数字键盘,但是您无法删除开头的数字.

Has anyone else come across this behavior with the text input coming up on first click rather than the number pad? Also does the two way binding on EditText work the way I am expecting. I have written my own Binding and InverseBinding adapter and they behave in the same way -> TextInput on first click and then number pad on second click but you cant delete the number that you start with.

推荐答案

如果您使用Android数据绑定库,则可以通过创建

If you use Android Databinding Library, it solves by creating binding adapter.

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;
        }
    }
}

这篇关于Android DataBinding浮动到TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆