在数据绑定中查看依赖于 CheckBox 的可见性
View visibility dependent on CheckBox in data binding
我想根据 CheckBox 选中状态设置视图可见性。就像我们在 preference.xml 中所做的那样。
目前我在做
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
>
<data>
<import type="android.view.View"/>
<variable
name="isScheduleChecked"
type="java.lang.Boolean"/>
<variable
name="activity"
type="com.amelio.ui.activities.ActivityCart"/>
</data>
<LinearLayout
style="@style/llDefault"
android:layout_height="match_parent"
android:orientation="vertical"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onCheckedChanged="@{()-> isScheduleChecked}"
android:text="Checkbox"/>
<LinearLayout
style="@style/llDefault"
android:padding="@dimen/space_small"
android:visibility="@{isScheduleChecked ? View.VISIBLE : View.GONE, default = gone}"
>
</LinearLayout>
</LinearLayout>
</layout>
这不起作用。我认为 android:onCheckedChanged="@{()-> isScheduleChecked}"
这条线不起作用。我做错了什么?有些人告诉我实现它的最佳方法。
目前我正在通过我的 activity 在 java 中更改 isScheduleChecked 代码,例如 binding.setIsScheduleChecked(true/false);
但我不会在 java class 中编写代码只需设置可见性。
这是个好主意!我通过将你的 onCheckedChanged
行替换为:
来让它工作
android:checked="@={isScheduleChecked}"
我以前不知道最简单的方法。
您可以参考数据绑定中的id。无需采用另一个变量。
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox"/>
<LinearLayout
style="@style/llDefault"
android:padding="@dimen/space_small"
android:visibility="@{checkbox.isChecked() ? View.VISIBLE : View.GONE, default = gone}"
>
</LinearLayout>
可能导致问题的原因
- ID 始终在
camelCase
中生成。比如 id 是 check_box
那么你将使用 checkBox.isChecked()
.
您必须在布局中导入 View
才能使用它 View.VISIBLE
<data>
<import type="android.view.View"/>
</data>
如果您有任何其他问题,可以发表评论。
对于那些尝试过 Khemraj Sharma 的解决方案但没有奏效的人,您可以试试这个,因为它对我有用。
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox"/>
<LinearLayout
style="@style/llDefault"
android:padding="@dimen/space_small"
android:visibility="@{checkbox.checked ? View.VISIBLE : View.GONE, default = gone}">
</LinearLayout>
从Khemraj Sharma的解决方案中,我修改的是数据绑定部分。我改了
发件人:
@{checkbox.isChecked() ? View.VISIBLE : View.GONE, default = gone}
收件人:
@{checkbox.checked ? View.VISIBLE : View.GONE, default = gone}
应该使用“checked”而不是“isChecked()”,因为“isChecked()”对我不起作用。
我想根据 CheckBox 选中状态设置视图可见性。就像我们在 preference.xml 中所做的那样。
目前我在做
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
>
<data>
<import type="android.view.View"/>
<variable
name="isScheduleChecked"
type="java.lang.Boolean"/>
<variable
name="activity"
type="com.amelio.ui.activities.ActivityCart"/>
</data>
<LinearLayout
style="@style/llDefault"
android:layout_height="match_parent"
android:orientation="vertical"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onCheckedChanged="@{()-> isScheduleChecked}"
android:text="Checkbox"/>
<LinearLayout
style="@style/llDefault"
android:padding="@dimen/space_small"
android:visibility="@{isScheduleChecked ? View.VISIBLE : View.GONE, default = gone}"
>
</LinearLayout>
</LinearLayout>
</layout>
这不起作用。我认为 android:onCheckedChanged="@{()-> isScheduleChecked}"
这条线不起作用。我做错了什么?有些人告诉我实现它的最佳方法。
目前我正在通过我的 activity 在 java 中更改 isScheduleChecked 代码,例如 binding.setIsScheduleChecked(true/false);
但我不会在 java class 中编写代码只需设置可见性。
这是个好主意!我通过将你的 onCheckedChanged
行替换为:
android:checked="@={isScheduleChecked}"
我以前不知道最简单的方法。
您可以参考数据绑定中的id。无需采用另一个变量。
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox"/>
<LinearLayout
style="@style/llDefault"
android:padding="@dimen/space_small"
android:visibility="@{checkbox.isChecked() ? View.VISIBLE : View.GONE, default = gone}"
>
</LinearLayout>
可能导致问题的原因
- ID 始终在
camelCase
中生成。比如 id 是check_box
那么你将使用checkBox.isChecked()
. 您必须在布局中导入
View
才能使用它View.VISIBLE
<data> <import type="android.view.View"/> </data>
如果您有任何其他问题,可以发表评论。
对于那些尝试过 Khemraj Sharma 的解决方案但没有奏效的人,您可以试试这个,因为它对我有用。
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox"/>
<LinearLayout
style="@style/llDefault"
android:padding="@dimen/space_small"
android:visibility="@{checkbox.checked ? View.VISIBLE : View.GONE, default = gone}">
</LinearLayout>
从Khemraj Sharma的解决方案中,我修改的是数据绑定部分。我改了
发件人:
@{checkbox.isChecked() ? View.VISIBLE : View.GONE, default = gone}
收件人:
@{checkbox.checked ? View.VISIBLE : View.GONE, default = gone}
应该使用“checked”而不是“isChecked()”,因为“isChecked()”对我不起作用。