Android中的数据绑定找不到具有java.langString的setter

问题描述 投票:0回答:1

我正在使用自定义组件进行数据绑定,并且在尝试将字符串绑定到xml中的值时遇到了困难。

这里是xml中的xml组件。问题是用progressText字符串替换了动态变量。我得到一个

找不到接受参数类型'java.lang.String'

尝试编译xml时出错。

            <com.mycompany.CircleProgressBar
                xmlns:cpb="http://schemas.android.com/apk/res-auto"
                android:id="@+id/progressWheel"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_centerInParent="true"
                android:layout_margin="10dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                cpb:progress="@{viewmodel.progress}"
                cpb:progressColor="@color/colorPrimary"
                cpb:backgroundColor="@color/gray"
                cpb:backgroundWidth="10"
                cpb:textSize="10sp"
                cpb:differenceInTextSizeInLines="10"
                cpb:roundedCorners="true"
                cpb:progressText="@{@string/work_package_percent_complete(viewmodel.progress)}"
                cpb:spaceBetweenLines="5"
                cpb:maxValue="100"
                cpb:progressTextColor="@color/colorPrimary" />

这是我声明在CircleProgressBar中使用的变量的方式:

@BindingAdapter("progressText")
fun setTextView(view: CircleProgressBar, value: String) {
    if (this::ta.isInitialized) {
        this.text = ta.getString(R.styleable.CircleProgressBar_progressText) as String
    } else {
        this.text = value
    }
    view.invalidate()
}

[当我尝试在xml文件的顶部为String进行导入语句时,我得到一个重复信息,即已经声明了String。如果我只输入纯文本而不替换字符串,则一切正常。

android-layout kotlin android-databinding
1个回答
0
投票

BindingAdapter似乎经常被滥用,因为它对于简单的装订来说是过大的杀伤力。想法是生成它们,而不是对其进行编程-并且具有预期名称的String getter / setter就足够了。为了公开预期的方法,只需要调用这些方法:

  • setProgressText(value: String)
  • getProgressText()

[CircleProgressBar是字段时,不必将其传递到方法中,保留期望的方法签名,而对于它的显式抱怨,缺少它。它甚至不必是视图模型中的字段,因为该模型不应依赖于视图。

© www.soinside.com 2019 - 2024. All rights reserved.