如何为MaterialButton指定自定义方法?

时间:2019-06-02 12:41:07

标签: android android-databinding

我在项目中使用数据绑定。当我编译项目时,编译器会向我显示此错误。

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
 android.databinding.tool.util.LoggedErrorException: Found data binding errors.
  ****/ data binding error ****msg:Cannot find the setter for attribute 'app:backgroundTint' with parameter type int on com.google.android.material.button.MaterialButton.

file:C:\Users\TikTak\AndroidStudioProjects\CarbonExample\app\src\main\res\>layout\activity_main.xml
  loc:21:30 - 21:109
  ****\ data binding error ****

起初,我确定了这样的适配器,但编译器再次向我显示此错误。

@BindingMethods(value = [
    BindingMethod(
        type = MaterialButton::class,
        attribute = "app:backgroundTint",
        method = "setBackgroundTintList")])
object MaterialButtonBindingAdapters {

}

我第二次这样编写适配器,但是编译器再次向我显示此错误。

@BindingMethods(value = [
    BindingMethod(
        type = MaterialButton::class,
        attribute = "app:backgroundTint",
        method = "setBackgroundTintList")])
object MaterialButtonBindingAdapters {
    @BindingAdapter("app:backgroundTint")
    @JvmStatic fun setBackgroundTintList(button: MaterialButton, color: ColorStateList) {
        button.backgroundTintList = tintList
    }
}

这是我的布局

<?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android">
        <data>
            <variable
                name="viewmodel"
                type="com.example.carbonexample.ViewModel" />
        </data>

        <LinearLayout 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"
            android:orientation="vertical"
            android:padding="16dp"
            tools:context=".MainActivity">

            <com.google.android.material.button.MaterialButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_margin="16dp"
                app:backgroundTint="@{viewmodel.isMine &amp;&amp; !viewmodel.isGuestMode ?  @color/white : @color/colorPrimary}"
                android:text="Test"
                android:textAllCaps="false"
                android:textColor="@color/black"
                app:cornerRadius="4dp"
                tools:text="Подключить акцию" />

            <!--<com.google.android.material.button.MaterialButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_margin="16dp"
                app:backgroundTint="@{viewmodel.isMine &amp;&amp; !viewmodel.isGuestMode ?  @color/white : @color/colorPrimary}"
                app:rippleColor="@{viewmodel.isMine &amp;&amp; !viewmodel.isGuestMode ?  @color/gray_trans_light : @color/colorPrimaryDarker}"
                app:strokeWidth="@{viewmodel.isMine &amp;&amp; !viewmodel.isGuestMode ?  @dimen/with_stroke : @dimen/no_stroke}"
                app:strokeColor="@color/gray_solid"
                android:enabled="@{viewmodel.enabled &amp;&amp; !viewmodel.progress}"
                android:onClick="@{v -> viewmodel.onActionButtonClicked()}"
                android:text="@{viewmodel.buttonCaption}"
                android:textAllCaps="false"
                android:textColor="@color/black"
                app:cornerRadius="4dp"
                android:visibility="@{viewmodel.buttonVisibility ? View.VISIBLE : View.GONE}"
                tools:text="Подключить акцию" />-->

            <Button
                android:id="@+id/btnIsMine"
                android:text="is mine"
                android:layout_width="wrap_content"
                android:onClick="funIsMine"
                android:layout_height="wrap_content"/>

            <Button
                android:id="@+id/btnEnabled"
                android:text="Button enabled"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
</layout>

我该如何解决这个问题?

0 个答案:

没有答案