【问题标题】:How to use Ternary condition inside a Ternaroy Operator in xml while setting data to a TextView with android databinding如何在xml中的三元运算符内使用三元条件,同时将数据设置为具有android数据绑定的TextView
【发布时间】:2018-12-12 18:08:45
【问题描述】:
 <TextView
    android:text="@{viewModel.sample.length>0?"first":"sorry"}"
    ---/>

上面的xml代码是为下面的条件设置文本,

if(sample.length>0){
  textId.setText("first")
}else{
  textId.setTex("sorry")
}

但是,对于下面的以下情况,我如何在 xml 中处理?

if(sample.length>0){
      textId.setText("first")
    }else if(sample.length>5){
      textId.setTex("second")
    }else{
      textId.setTex("sorry")
    }

【问题讨论】:

    标签: android xml data-binding viewmodel


    【解决方案1】:

    我们也可以在另一个三元运算符中使用三元条件。

    你可以像这样使用三元运算符:-

       <TextView
            android:text="@{viewModel.sample.length>5 ? "second" : viewModel.sample.length>0?"first":"sorry"}"
            ----/>
    

    【讨论】:

    • 支持我的问题@Thunder,它将帮助更多人看到
    【解决方案2】:

    试试嵌套三元运算符

    <TextView
        android:text="@{viewModel.sample.length>0?(@{viewModel.sample.length>5?"second":"first"):"sorry"}"
        --------------------------------/>
    

    【讨论】:

      猜你喜欢
      • 2018-10-18
      • 2018-01-07
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      • 2021-08-01
      • 1970-01-01
      • 2010-09-28
      相关资源
      最近更新 更多