gpt4 book ai didi

Android 数据绑定(bind)默认变量值

转载 作者:行者123 更新时间:2023-11-29 16:33:41 25 4
gpt4 key购买 nike

我有这个布局。

<data>
<import type="android.view.View" />
<variable
name="shouldBeVisible"
type="java.lang.Boolean" />
</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="@={shouldBeVisible}" />

<LinearLayout
android:layout_width="40dp"
android:layout_height="30dp"
android:background="#dd0"
android:visibility="@{shouldBeVisible ? View.VISIBLE : View.GONE}" />

</LinearLayout>

shouldBeVisible 默认为 false。 有没有办法使 shouldBeVisible 为真?在这种情况下,我想让 LinearLayout 可见。

到目前为止,我正在使用 binding.shouldBeVisible=true

最佳答案

要解决该错误,您可以使用多种方法:

  1. 您可以使用具有反转逻辑的原始 bool 类型(shouldBeVisible -> shouldBeHidden)

  2. 第二种方法是使用装箱的 bool 类型(就像您现在使用的那样)并在表达式中设置默认值

  3. 第三种方式 - 在绑定(bind)膨胀后手动设置它(正如您现在所做的那样)

    binding.shouldBeVisible=true

  4. 在数据绑定(bind)值中使用默认关键字

选择最符合您需求的一项。

关于Android 数据绑定(bind)默认变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53270107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com