为什么 CheckBox 选中不能以编程方式与 Kotlin 一起使用?
Why CheckBox checked not working with Kotlin programmatically?
我想这个问题可能以前有人问过,但我也遇到过这个问题,所以我再次在这里问,看看我们能找到一些解决方案。
所以基本上问题是以编程方式检查 Checkbox 不使用 Kotlin 代码。为了解释,我正在分享我的代码和问题截图。
//filterContraints is ArrayList<Integer> type
if(filterContraints!!.size > 0){
filterContraints!!.forEach {
Log.d(TAG, "For each filter constraints : $it")
if(it == AppConstants.MAC_OS && !chkMacOS!!.isChecked)
{
chkMacOS!!.isChecked = true
}
if(it == AppConstants.WINDOWS_OS && !chkWindows!!.isChecked)
{
chkWindows!!.isChecked = true
}
if(it == AppConstants.LINUX_OS && !chkLinux!!.isChecked)
{
chkLinux!!.isChecked = true
}
if(it == AppConstants.ALL_OS && !chkAllOs!!.isChecked)
{
chkAllOs!!.isChecked = true
}
}
}
这是整个布局的 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/white"
android:clickable="true"
android:clipToPadding="false"
android:fitsSystemWindows="true"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="@string/bottom_sheet_behavior">
<TextView
android:id="@+id/txt_filter_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@color/darkestGrey"
android:layout_marginTop="@dimen/padd_10"
android:text="@string/txt_filter_payload"
android:fontFamily="sans-serif-medium"
android:textStyle="bold"
android:textSize="18sp" />
<LinearLayout
android:id="@+id/row_filter_categories"
android:layout_width="wrap_content"
android:layout_below="@+id/txt_filter_title"
android:layout_marginTop="@dimen/padd_10"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/chk_mac"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="@string/chk_os_mac" />
<CheckBox
android:id="@+id/chk_windows"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/padd_10"
android:layout_marginEnd="@dimen/padd_10"
android:textSize="15sp"
android:text="@string/chk_os_windows" />
<CheckBox
android:id="@+id/chk_linux"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="@string/chk_os_linux" />
<CheckBox
android:id="@+id/chk_all_os"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textAllCaps="false"
android:text="@string/chk_os_all" />
</LinearLayout>
<Button
android:id="@+id/btn_apply_filter"
android:layout_width="wrap_content"
android:layout_below="@+id/row_filter_categories"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginEnd="@dimen/padd_10"
android:layout_alignParentEnd="true"
android:gravity="center"
android:background="@color/colorPrimary"
android:textColor="@color/white"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="@string/txt_apply_filter" />
</RelativeLayout>
这是 Kotlin 中的声明部分:
private var chkMacOS: CheckBox? = null
private var chkWindows: CheckBox? = null
private var chkLinux: CheckBox? = null
private var chkAllOs: CheckBox? = null
这里是检索部分:
chkMacOS = fragmentView.findViewById(R.id.chk_mac)
chkWindows = fragmentView.findViewById(R.id.chk_windows)
chkLinux = fragmentView.findViewById(R.id.chk_linux)
chkAllOs = fragmentView.findViewById(R.id.chk_all_os)
这里是输出:见最后两个复选框,只有边框是彩色的但没有被选中。
我认为 kotlin 不支持复选框检查。
在您的情况下无法正常工作。
你一定在你的代码中做了一些混乱的事情,因为我们只能看到代码的和平而不确定你的代码有什么问题。上面的代码很好,否则应该可以工作,请检查您创建复选框的方式。
Also use latest stable release of kotlin always.
好的,我得到了答案。实际上有两个问题,这是我在寻找解决方案时发现的:
- 此问题与 Kotlin 无关,Java 中也有此问题。
- setChecked 适用于 Android 5.0 但不适用于 Nougat
以下是我参考的一些参考资料:
A) 仅部分选中单选按钮
B) Android Nougat:为什么以编程方式选择 Fragment 上的复选框时状态不完整(但在 Lollipop 上看起来不错)
这是一个适用于两个 android 版本的解决方案:
chkMacOS!!.post {
chkMacOS!!.setChecked(true)
chkMacOS!!.jumpDrawablesToCurrentState() // This is most important
}
我希望这对正在寻找类似解决方案的任何人有所帮助(thx @Nipper)
我想这个问题可能以前有人问过,但我也遇到过这个问题,所以我再次在这里问,看看我们能找到一些解决方案。
所以基本上问题是以编程方式检查 Checkbox 不使用 Kotlin 代码。为了解释,我正在分享我的代码和问题截图。
//filterContraints is ArrayList<Integer> type
if(filterContraints!!.size > 0){
filterContraints!!.forEach {
Log.d(TAG, "For each filter constraints : $it")
if(it == AppConstants.MAC_OS && !chkMacOS!!.isChecked)
{
chkMacOS!!.isChecked = true
}
if(it == AppConstants.WINDOWS_OS && !chkWindows!!.isChecked)
{
chkWindows!!.isChecked = true
}
if(it == AppConstants.LINUX_OS && !chkLinux!!.isChecked)
{
chkLinux!!.isChecked = true
}
if(it == AppConstants.ALL_OS && !chkAllOs!!.isChecked)
{
chkAllOs!!.isChecked = true
}
}
}
这是整个布局的 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/white"
android:clickable="true"
android:clipToPadding="false"
android:fitsSystemWindows="true"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="@string/bottom_sheet_behavior">
<TextView
android:id="@+id/txt_filter_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@color/darkestGrey"
android:layout_marginTop="@dimen/padd_10"
android:text="@string/txt_filter_payload"
android:fontFamily="sans-serif-medium"
android:textStyle="bold"
android:textSize="18sp" />
<LinearLayout
android:id="@+id/row_filter_categories"
android:layout_width="wrap_content"
android:layout_below="@+id/txt_filter_title"
android:layout_marginTop="@dimen/padd_10"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/chk_mac"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="@string/chk_os_mac" />
<CheckBox
android:id="@+id/chk_windows"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/padd_10"
android:layout_marginEnd="@dimen/padd_10"
android:textSize="15sp"
android:text="@string/chk_os_windows" />
<CheckBox
android:id="@+id/chk_linux"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="@string/chk_os_linux" />
<CheckBox
android:id="@+id/chk_all_os"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textAllCaps="false"
android:text="@string/chk_os_all" />
</LinearLayout>
<Button
android:id="@+id/btn_apply_filter"
android:layout_width="wrap_content"
android:layout_below="@+id/row_filter_categories"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginEnd="@dimen/padd_10"
android:layout_alignParentEnd="true"
android:gravity="center"
android:background="@color/colorPrimary"
android:textColor="@color/white"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="@string/txt_apply_filter" />
</RelativeLayout>
这是 Kotlin 中的声明部分:
private var chkMacOS: CheckBox? = null
private var chkWindows: CheckBox? = null
private var chkLinux: CheckBox? = null
private var chkAllOs: CheckBox? = null
这里是检索部分:
chkMacOS = fragmentView.findViewById(R.id.chk_mac)
chkWindows = fragmentView.findViewById(R.id.chk_windows)
chkLinux = fragmentView.findViewById(R.id.chk_linux)
chkAllOs = fragmentView.findViewById(R.id.chk_all_os)
这里是输出:见最后两个复选框,只有边框是彩色的但没有被选中。
我认为 kotlin 不支持复选框检查。 在您的情况下无法正常工作。 你一定在你的代码中做了一些混乱的事情,因为我们只能看到代码的和平而不确定你的代码有什么问题。上面的代码很好,否则应该可以工作,请检查您创建复选框的方式。
Also use latest stable release of kotlin always.
好的,我得到了答案。实际上有两个问题,这是我在寻找解决方案时发现的:
- 此问题与 Kotlin 无关,Java 中也有此问题。
- setChecked 适用于 Android 5.0 但不适用于 Nougat
以下是我参考的一些参考资料:
A) 仅部分选中单选按钮
B) Android Nougat:为什么以编程方式选择 Fragment 上的复选框时状态不完整(但在 Lollipop 上看起来不错)
这是一个适用于两个 android 版本的解决方案:
chkMacOS!!.post {
chkMacOS!!.setChecked(true)
chkMacOS!!.jumpDrawablesToCurrentState() // This is most important
}
我希望这对正在寻找类似解决方案的任何人有所帮助(thx @Nipper)