TextView.
Android, , System.out.format( "% 02d" , n);?
DataBinding, , origin android: text ( ) ().
Android -
(https://developer.android.com/topic/libraries/data-binding/index.html#expression_language)
android:text="@{@string/nameFormat(firstName, lastName)}"
android:text="@{@plurals/banana(bananaCount)}"
But if you want it in a more flexible way, you need to write your own binding methods, as shown below.
In your code
@BindingAdapter(value = {"android:text", "android:formatArgs"}, requireAll = false)
public static void bindTextStr(TextView tv, String text, Object[] formatstr) {
if (formatstr == null) {
tv.setText(text);
} else {
tv.setText(String.format(text, formatstr));
}
}
In your xml layout,
<variable
name="normalStr"
type="java.lang.String"/>
<variable name="data" type="Object[]"/>
...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{normalStr}"
android:formatArgs="@{data}"
/>
In action code
mBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.activity_main, null, false);
mBinding.setNormalStr("Data : 1(%s), 2(%d)");
mBinding.setData(new Object[]{"Hello World",12345});
source
share