的Android綁定適配器我有一個綁定的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顯示多個錯誤。我已經將綁定適配器和逆向綁定適配器的屬性從「android:text」更改爲「text」,但它不起作用。在這個問題中,另一個用戶發生了類似的事情:Data Binding broke after upgrade to Gradle 2.3但區別在於我使用「android:text」進行綁定,所以George Mount給出的答案對我來說不起作用。任何人有任何想法?我非常沮喪,因爲我無法更新Android Studio因爲這個問題。
難道你BindingAdapter使用「文本」,而不是「機器人:文字」? –
當我使用Android Studio 2.2.3並在綁定和反向綁定適配器中使用android:text時,它不顯示錯誤,但在2.3及更高版本中,它顯示找不到錯誤(雙精度或整數) –
@GeorgeMount回答。所以你知道我也嘗試過使用android:text =「''+ object.double」,並且它在Android Studio 2.3和更高版本中沒有顯示錯誤,但是雙向綁定不能正常工作 –