2016-01-30 111 views
1

我應該能夠在Android dataBinding中使用@BindingAdapter,以便我可以覆蓋某個屬性。我可以用cusutom屬性來完成它,但是使用android內置屬性,它是如何完成的?如何覆蓋按鈕屬性

是我到目前爲止有: 在我的ViewModel我有一個標註有@BindingAdapter,看起來像這樣的方法:

@BindingAdapter({"android:text"}) 
    public static void setText(Button view,boolean language) {//i need to pass one more variable in here for area code , its just a integer, but how ? 

     if("french".equals(language))//i want to test if french && area code 
      view.setText("si vous play"); 
    else if ("English".equals(language)) //i want to test if french && area code 
     view.setText("please"); 

    } 

,但我有幾個問題。讓我們看看下面的XML:

<Button 
     android:id="@+id/mybutton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@{`french,844`}"/> 

看到我的問題,我想傳入多個參數到dataBinding。但是如何?我是否必須製作POJO對象並將其傳入?即使我這樣做,我如何設置從XML的對象?

所以,如果有人可以告訴我以下我會很好: 1.如何將多個值傳遞給bindingAdapter和 2.如何覆蓋任何視圖中的內置android屬性。

+0

順便說一句,「請」是「s'il vous辮子」而不是「si vous play」:) –

回答

1

如果你想在你BindingAdapter,你應該使用兩種不同的屬性設置兩個不同的值:

<Button 
    android:id="@+id/mybutton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@{`french`}" 
    app:areaCode="@{`844`}"/> 

然後有兩個在你BindingAdapter不同的屬性:

@BindingAdapter({"android:text", "areaCode"}) 
public static void setText(Button view, String language, String areaCode) { 
    ... 
} 

但它可能會更好地設置一個不同的「應用程序:語言」,這對開發人員來說會更清楚。