InverseBindingAdapter not working, Getting error: <identifier> expected in generated Java code
InverseBindingAdapter not working, Getting error: <identifier> expected in generated Java code
我正在尝试创建自定义绑定适配器以将 EditText
文本转换为长文本和反向文本。
这是我的绑定适配器class
class CustomBindingUtils {
@BindingAdapter("android:text")
fun setLong(editText: EditText, value: Long) {
editText.setText(value.toString())
}
@InverseBindingAdapter(attribute = "android:text")
fun getLong(editText: EditText): Long {
val textValue = editText.text.toString()
if (textValue.isDigitsOnly()) {
return textValue.toLong()
}
return 0L
}
}
使用自定义绑定适配器的视图:
<EditText
android:id="@+id/payeeAmountInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/bg_rounded_green"
android:inputType="numberDecimal"
android:padding="8dp"
android:text="@={payee.amount}"
android:textAlignment="textEnd"
app:layout_constraintLeft_toRightOf="@id/payeeNameInput"
app:layout_constraintRight_toLeftOf="@id/removePayeeButton"
app:layout_constraintTop_toTopOf="parent" />
但是当我点击构建时,生成的 Java class.
出现以下错误
/home/xxxxx/Works/AppTest/app/build/generated/source/kapt/debug/com/xxxxxx/groupsplit/databinding/LayoutPayeeItemBindingImpl.java:31: error: <identifier> expected
long callbackArg_0 = mBindingComponent.null.getLong(payeeAmountInput);
^
我是不是做错了什么?
您需要将 BindingAdapter
包装到伴生对象中并注释 @JvmStatic
:
companion object {
@BindingAdapter("android:text")
@JvmStatic
fun setLong(editText: EditText, value: Long) {
editText.setText(value.toString())
}
}
我正在尝试创建自定义绑定适配器以将 EditText
文本转换为长文本和反向文本。
这是我的绑定适配器class
class CustomBindingUtils {
@BindingAdapter("android:text")
fun setLong(editText: EditText, value: Long) {
editText.setText(value.toString())
}
@InverseBindingAdapter(attribute = "android:text")
fun getLong(editText: EditText): Long {
val textValue = editText.text.toString()
if (textValue.isDigitsOnly()) {
return textValue.toLong()
}
return 0L
}
}
使用自定义绑定适配器的视图:
<EditText
android:id="@+id/payeeAmountInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/bg_rounded_green"
android:inputType="numberDecimal"
android:padding="8dp"
android:text="@={payee.amount}"
android:textAlignment="textEnd"
app:layout_constraintLeft_toRightOf="@id/payeeNameInput"
app:layout_constraintRight_toLeftOf="@id/removePayeeButton"
app:layout_constraintTop_toTopOf="parent" />
但是当我点击构建时,生成的 Java class.
出现以下错误/home/xxxxx/Works/AppTest/app/build/generated/source/kapt/debug/com/xxxxxx/groupsplit/databinding/LayoutPayeeItemBindingImpl.java:31: error: <identifier> expected
long callbackArg_0 = mBindingComponent.null.getLong(payeeAmountInput);
^
我是不是做错了什么?
您需要将 BindingAdapter
包装到伴生对象中并注释 @JvmStatic
:
companion object {
@BindingAdapter("android:text")
@JvmStatic
fun setLong(editText: EditText, value: Long) {
editText.setText(value.toString())
}
}