DataBinding笔记5

2020-08-09  本文已影响0人  satisfying

数据自动转换

  <variable
            name="time"
            type="java.util.Date" />

        <variable
            name="bg"
            type="java.lang.String" />

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{time}" />
 <TextView
     android:layout_width="20dp"
     android:layout_height="20dp"
     android:background="@{bg}"
            />


@BindingConversion
fun convertDate(date: Date?): String? {
    val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
    return sdf.format(date)
}
@BindingConversion
fun converColor(value: String?): Drawable? {
    return ColorDrawable(Color.parseColor(value))
}


        bindingBinding.time= Date()
        bindingBinding.bg="#ffeeff"
上一篇下一篇

猜你喜欢

热点阅读