我有一个名为Item
的对象public class Item extends BaseObservable
{
private double dose;
private Integer reportedDose;
@Bindable
public double getDose() {
return dose;
}
public void setDose(double dose) {
this.dose = dose;
notifyPropertyChanged(BR.dose);
}
}
现在关注@GeorgeMount上一篇教程:https://medium.com/google-developers/android-data-binding-inverse-functions-95aab4b11873我添加了Converter类:
public class Converter
{
@InverseMethod("toDouble")
public static String toString(TextView view, double oldValue,
double value) {
NumberFormat numberFormat = getNumberFormat(view);
try {
// Don't return a different value if the parsed value
// doesn't change
String inView = view.getText().toString();
double parsed =
numberFormat.parse(inView).doubleValue();
if (parsed == value) {
return view.getText().toString();
}
} catch (ParseException e) {
// Old number was broken
}
return numberFormat.format(value);
}
public static double toDouble(TextView view, double oldValue,
String value) {
NumberFormat numberFormat = getNumberFormat(view);
try {
return numberFormat.parse(value).doubleValue();
} catch (ParseException e) {
Resources resources = view.getResources();
//String errStr = resources.getString(R.string.badNumber);
view.setError("error");
return oldValue;
}
}
private static NumberFormat getNumberFormat(View view) {
Resources resources= view.getResources();
Locale locale = resources.getConfiguration().locale;
NumberFormat format =
NumberFormat.getNumberInstance(locale);
if (format instanceof DecimalFormat) {
DecimalFormat decimalFormat = (DecimalFormat) format;
decimalFormat.setGroupingUsed(false);
}
return format;
}
}
现在我正在尝试在我的activity_main.xml中的EditText中使用它:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="item"
type="com.example.dan.bindtest.Item" />
<variable
name="Converter"
type="com.example.dan.bindtest.Converter"/>
</data>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.dan.bindtest.MainActivity"
android:padding="30dp">
<EditText
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:text="@={Converter.toString(first, item.dose, item.dose)}"
android:hint="Enter a dose..."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:hint="Enter a dose..."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</layout>
但问题在于,即使我按照一步一步的说明执行此错误:
"Error:cannot generate view binders java.lang.ClassCastException: android.databinding.tool.expr.IdentifierExpr cannot be cast to android.databinding.tool.expr.StaticIdentifierExpr"
如果我从EditText中删除android:text = ...行,则编译时没有错误。此外,我试图删除@Bindable注释和notifyPropertyChanged(BR.dose)行仍然有同样的问题。我有这个代码在公共存储库中,如果有人想要检查它:https://github.com/danponce/bindtest任何人都有线索? @GeorgeMount?
答案 0 :(得分:7)
我认为您已经声明了静态方法,因此您无法使用类对象来访问静态方法。 你应该使用Classname来访问静态方法。
<import type="com.example.dan.bindtest.Converter"/>
然后使用:
android:text="@={Converter.toString(first, item.dose, item.dose)}"