为什么CheckBox不能通过编程方式与Kotlin一起使用? [英] Why CheckBox checked not working with Kotlin programmatically?
问题描述
我认为这个问题可能是之前提出的,但是这个问题也发生在我身上,所以,我再次在这里问我们是否可以找到解决方案.
I think this question may be asked before but this issue happened with me too so , I'm asking here again to see we can get some solution over it.
因此,基本上的问题是使Checkbox以编程方式无法与Kotlin代码一起使用.为了说明,我在分享我的代码&问题的屏幕截图.
So basically problem is making Checkbox checked Programmatically not working with Kotlin code . To explain , I'm sharing my code & screenshot of problem.
//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:
Here is xml of entire layout :
<?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中的声明部分:
And here is declarations part in 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)
这是输出:请参见最后两个复选框,只有边框是彩色的但未被选中.
And here is output: See last two checkbox , only border is coloured but not selected.
推荐答案
好的,我得到了答案.实际上,有两个问题是我在找到解决方案时发现的:
Okay , I got an answer. Actually there were two issues , which I have found during finding solution :
- 此问题与Kotlin无关,这也发生在Java中.
- setChecked可用于Android 5.0,但不适用于Nougat
以下是我引用的参考文献:
Here are few reference which I referred :
B) Android牛轧糖:以编程方式选择时,为什么Fragment上的复选框的状态不完整(但在Lollipop上看起来不错)
这是一个适用于我的两个Android版本的解决方案:
And here is a solution which works for me on both android versions:
chkMacOS!!.post {
chkMacOS!!.setChecked(true)
chkMacOS!!.jumpDrawablesToCurrentState() // This is most important
}
我希望这对正在寻求类似解决方案的人(thx @Nipper)有帮助
I hope this helps anyone who is looking for similar kind of solution (thx @Nipper)
这篇关于为什么CheckBox不能通过编程方式与Kotlin一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!