如何将方法的返回绑定到TextView?
样品:
class U {
final String a = "x", b = "ddd";
String message(){
return a + " " + b;
}
}
所以通过数据绑定我想显示message()
的返回值答案 0 :(得分:4)
尝试这样的事情
1 - 在您的活动中:
YourLayoutBinding mBinding; //this type of class is auto-generated by AS based on the name of your layout.xml
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.your_layout);
mBinding.setU(new U());
}
2 - 在您的布局中:
<data>
<variable name="u" type="U" />
</data>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@{u.message}"
android:textColor="#000"/>