gpt4 book ai didi

Android BindingConversion 多个参数

转载 作者:行者123 更新时间:2023-11-30 00:53:12 25 4
gpt4 key购买 nike

我写了这个 BindingConversion:

@BindingConversion
public static String convertDateToFormat(Date date, String format) {
return new SimpleDateFormat(format).format(date);
}

在我的布局文件中:

<TextView
android:text="@{BindingUtils.convertDateToFormat(schedule.dateStart, `EEEE`)}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"/>

但是构建失败。

谁能解释一下如何将多个参数传递给和绑定(bind)转换?

最佳答案

来自 Android documation

The converter should take one parameter, the expression type, and the return value should be the target value type used in the setter

目前不支持有参数。

您可以改为使用 BindingAdapter:

@BindingAdapter({ "date", "format" })
public static void bindDate(final TextView view, final ZonedDateTime date, final String format) {
if (date != null) {
view.setText(DateTimeFormatter.ofPattern(format).format(date));
}
}

关于Android BindingConversion 多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40595770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com