0

的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因爲這個問題。

+0

難道你BindingAdapter使用「文本」,而不是「機器人:文字」? –

+0

當我使用Android Studio 2.2.3並在綁定和反向綁定適配器中使用android:text時,它不顯示錯誤,但在2.3及更高版本中,它顯示找不到錯誤(雙精度或整數) –

+0

@GeorgeMount回答。所以你知道我也嘗試過使用android:text =「''+ object.double」,並且它在Android Studio 2.3和更高版本中沒有顯示錯誤,但是雙向綁定不能正常工作 –

回答

0

你要像下面

<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:setText="@{workOrderFertilizer.dose}"/> 

東西適配器

@BindingAdapter("setText") 
public static void bindDoubleInText(AppCompatEditText tv, Object 
d) 
{ 
    Double value = (Double) d; 
    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)); 
    } 
} 
+0

但是這個適配器只會在添加布局時觸發,當文本改變時不會觸發 –

1

的快捷鍵爲雙向綁定,如android:text='@={"" + obj.val}'只與原語,而不是與他們的對象封裝類,如double和float工作。

您可以使用BindingAdapters對包裝進行雙向綁定。我寫這些,他們似乎好了工作:

@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 3.0,您還可以使用轉換功能,但除非你做一些管理的輸入變爲有點靠不住。你可以像這樣將它綁定:

<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(); 
} 
+0

感謝您的回答!我會嘗試使用綁定適配器,如果它能正常工作,我會通知您 –

+0

在這種情況下,實現轉換器的優點是什麼?以及它們如何導入到xml佈局? –

+0

沒有優勢。在這種情況下最好使用綁定適配器。 –