我在 Binding Utils 类中有这个适配器:
@BindingAdapter("text")
public static void bindDoubleInText(AppCompatEditText tv, Double
value)
{
if (tv.getText() == null) return;
// Get the actual EditText value
String edittextValue = tv.getText().toString();
if (edittextValue.isEmpty())
{
if (value != null) tv.setText(Double.toString(value));
}
else
{
if (value != null &&
Double.parseDouble(tv.getText().toString()) != value)
tv.setText(Double.toString(value));
}
}
@InverseBindingAdapter(attribute = "android:text")
public static Double getDoubleFromBinding(TextView view)
{
String string = view.getText().toString();
return string.isEmpty() ? null : Double.parseDouble(string);
}
@BindingAdapter("text")
public static void bindIntegerInText(AppCompatEditText
appCompatEditText, Integer value)
{
CharSequence charSequence = appCompatEditText.getText();
if (charSequence == null || value == null) return;
// Get the actual EditText value
String edittextValue = charSequence.toString();
if (edittextValue.isEmpty() || Integer.parseInt(edittextValue) !=
value)
appCompatEditText.setText(Integer.toString(value));
}
@InverseBindingAdapter(attribute = "android:text")
public static Integer getIntegerFromBinding(TextView view)
{
String string = view.getText().toString();
return string.isEmpty() ? null : Integer.parseInt(string);
}`
这是我的一个 xml 文件的一部分:
<android.support.v7.widget.AppCompatEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:gravity="center_horizontal"
android:hint="dosis"
android:inputType="numberDecimal"
android:minEms="5"
android:text="@={workOrderFertilizer.dose}"/>`
问题是 Android Studio 2.2.3 版本运行良好。当我更新到 Android Studio 2.3 时,它停止工作并且 Gradle 显示多个错误。我已将绑定(bind)适配器和反向绑定(bind)适配器的属性从“android:text”更改为“text”,但它不起作用。在这个问题中,另一个用户也发生了类似的事情:Data Binding broke after upgrade to Gradle 2.3但不同之处在于我使用“android:text”进行绑定(bind),因此 George Mount 给出的答案对我不起作用。有人有什么想法吗?我对此感到非常沮丧,因为这个问题导致我无法更新 Android Studio。
最佳答案
双向绑定(bind)的快捷方式,如 android:text='@={""+ obj.val}'
仅适用于原语,不适用于它们的对象包装器,如 Double 和 Float。
您可以使用带有 BindingAdapter 的包装器进行双向绑定(bind)。我写了这些,它们似乎工作正常:
@BindingAdapter("android:text")
public static void setDouble(TextView view, Double val) {
if (val != null) {
String currentValue = view.getText().toString();
if (currentValue.length() != 0) {
try {
double oldVal = Double.parseDouble(currentValue);
if (oldVal == val) {
return;
}
} catch (NumberFormatException e) {
// that's ok, we can just set the value.
}
}
view.setText(val.toString());
}
}
@InverseBindingAdapter(attribute = "android:text")
public static Double getDouble(TextView view) {
try {
return Double.parseDouble(view.getText().toString());
} catch (NumberFormatException e) {
return null;
}
}
使用 Android gradle plugin 3.0,您还可以使用转换功能,但除非您进行一些管理,否则输入会有点不稳定。你可以像这样绑定(bind)它:
<EditText
android:id="@+id/textView"
android:text="@={Converters.doubleToString(textView, item.dose)}" .../>
并有这样的转换器:
@InverseMethod("doubleToString")
public static Double stringToDouble(TextView view, CharSequence value) {
if (value == null) {
return null;
}
try {
return Double.parseDouble(value.toString());
} catch (NumberFormatException e) {
return null;
}
}
public static CharSequence doubleToString(TextView view, Double value) {
if (value == null) {
return "";
}
String oldText = view.getText().toString();
if (!oldText.isEmpty()) {
try {
double oldVal = Double.parseDouble(oldText);
if (oldVal == value) {
return view.getText();
}
} catch (NumberFormatException e) {
// No problem. The TextView had text that wasn't formatted properly
}
}
return value.toString();
}
关于Android 数据绑定(bind)适配器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45397799/