【问题标题】:Call method in data binding数据绑定中的调用方法
【发布时间】:2017-08-31 14:07:54
【问题描述】:

如何将方法的返回绑定到 TextView 中?

示例:

class U {
    final String a = "x", b = "ddd";

    String message(){
        return a + " " + b;
    }
}

所以通过数据绑定,我想显示 message() 的返回

【问题讨论】:

    标签: android data-binding android-databinding


    【解决方案1】:

    试试这样的

    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"/>
    

    【讨论】:

    • 并且类和方法也必须是公共的。
    • u.message 中的“消息”是什么?方法在哪里?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    相关资源
    最近更新 更多