简体   繁体   English

如何确保不会同时选中多个复选框? 在科特林

[英]How can I make sure that more than one checkbox is not selected at the same time? in kotlin

I would like to know how I can do so that if one checkbox is selected the other is deactivated as it would?我想知道我该怎么做才能在选中一个复选框时禁用另一个复选框? Here I have the base code in kotlin that I have now, what is the best way to do it?, And I wanted to know if there are topics for checkboxes how to make a round checkbox or others.在这里,我有我现在拥有的 kotlin 基本代码,最好的方法是什么?,我想知道是否有关于复选框的主题如何制作圆形复选框或其他。

class MinOrMax : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_min_or_max)
    }

    fun onCheckboxClicked(view: View)
    {
        if (view is CheckBox)
        {
            val checked: Boolean = view.isChecked
            when (view.id)
            {
                R.id.select_max -> {
                    if (checked) {
                        
                    } else {
                        // Remove the meat
                    }
                }
                R.id.select_min -> {
                    if (checked) {
                        // Cheese me
                    } else {
                        // I'm lactose intolerant
                    }
                }
            }
        }
    }
}

I also want to know how I have to call this function onCheckboxClicked, could you order it for me please.我也想知道如何调用这个函数 onCheckboxClicked,你能帮我订购吗? xml: xml:

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/min_or_max_checkbox"
        android:layout_marginTop="20dp"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintVertical_bias="0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/textView6"
        app:layout_constraintTop_toBottomOf="@+id/textView6"
        android:orientation="vertical"
        android:padding="8dp"
        >
        <CheckBox
            android:id="@+id/select_min"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Maximizar?"
            android:textColor="@color/colorWhite"
            android:textSize="20dp"
            android:textStyle="bold"
            android:padding="7dp"
            android:textColorHighlight="@color/colorWhite"
            android:onClick="onCheckboxClicked"
            android:checked="true"
            android:buttonTint="@color/colorCheckBox"
            />
        <CheckBox
            android:id="@+id/select_max"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Minimizar"
            android:textColor="@color/colorWhite"
            android:textSize="20dp"
            android:textStyle="bold"
            android:padding="8dp"
            android:onClick="onCheckboxClicked"
            android:buttonTint="@color/colorCheckBox"
            />
    </LinearLayout>

You can use radiobutton and style it like a checkbox.您可以使用单选按钮并将其样式设置为复选框。 follow these steps :按着这些次序 :

  1. copy these xml files in /res/drawable directory将这些 xml 文件复制到 /res/drawable 目录中

btn_check_material_anim.xml btn_check_material_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/checked"
        android:state_checked="true"
        android:drawable="@drawable/btn_checkbox_checked_mtrl" />
    <item
        android:id="@+id/unchecked"
        android:drawable="@drawable/btn_checkbox_unchecked_mtrl" />
    <transition
        android:fromId="@+id/unchecked"
        android:toId="@+id/checked"
        android:drawable="@drawable/btn_checkbox_unchecked_to_checked_mtrl_animation" />
    <transition
        android:fromId="@+id/checked"
        android:toId="@+id/unchecked"
        android:drawable="@drawable/btn_checkbox_checked_to_unchecked_mtrl_animation" />
</animated-selector>

btn_checkbox_checked_mtrl.xml btn_checkbox_checked_mtrl.xml

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="btn_checkbox_checked"
    android:width="32dp"
    android:viewportWidth="48"
    android:height="32dp"
    android:viewportHeight="48"
    android:tint="@color/control_checkable_material">
    <group
        android:name="icon_null"
        android:translateX="24"
        android:translateY="24"
        android:scaleX="0.2"
        android:scaleY="0.2">
        <group
            android:name="check"
            android:scaleX="7.5"
            android:scaleY="7.5">
            <path
                android:name="check_path_merged"
                android:pathData="M 7.0,-9.0 c 0.0,0.0 -14.0,0.0 -14.0,0.0 c -1.1044921875,0.0 -2.0,0.8955078125 -2.0,2.0 c 0.0,0.0 0.0,14.0 0.0,14.0 c 0.0,1.1044921875 0.8955078125,2.0 2.0,2.0 c 0.0,0.0 14.0,0.0 14.0,0.0 c 1.1044921875,0.0 2.0,-0.8955078125 2.0,-2.0 c 0.0,0.0 0.0,-14.0 0.0,-14.0 c 0.0,-1.1044921875 -0.8955078125,-2.0 -2.0,-2.0 c 0.0,0.0 0.0,0.0 0.0,0.0 Z M -2.0,5.00001525879 c 0.0,0.0 -5.0,-5.00001525879 -5.0,-5.00001525879 c 0.0,0.0 1.41409301758,-1.41409301758 1.41409301758,-1.41409301758 c 0.0,0.0 3.58590698242,3.58601379395 3.58590698242,3.58601379395 c 0.0,0.0 7.58590698242,-7.58601379395 7.58590698242,-7.58601379395 c 0.0,0.0 1.41409301758,1.41409301758 1.41409301758,1.41409301758 c 0.0,0.0 -9.0,9.00001525879 -9.0,9.00001525879 Z"
                android:fillColor="#FF000000" />
        </group>
        <group
            android:name="box_dilate"
            android:scaleX="7.5"
            android:scaleY="7.5">
            <path
                android:name="box_inner_merged"
                android:pathData="M 0.0,-1.0 l 0.0,0.0 c 0.5522847498,0.0 1.0,0.4477152502 1.0,1.0 l 0.0,0.0 c 0.0,0.5522847498 -0.4477152502,1.0 -1.0,1.0 l 0.0,0.0 c -0.5522847498,0.0 -1.0,-0.4477152502 -1.0,-1.0 l 0.0,0.0 c 0.0,-0.5522847498 0.4477152502,-1.0 1.0,-1.0 Z M 7.0,-9.0 c 0.0,0.0 -14.0,0.0 -14.0,0.0 c -1.1044921875,0.0 -2.0,0.8955078125 -2.0,2.0 c 0.0,0.0 0.0,14.0 0.0,14.0 c 0.0,1.1044921875 0.8955078125,2.0 2.0,2.0 c 0.0,0.0 14.0,0.0 14.0,0.0 c 1.1044921875,0.0 2.0,-0.8955078125 2.0,-2.0 c 0.0,0.0 0.0,-14.0 0.0,-14.0 c 0.0,-1.1044921875 -0.8955078125,-2.0 -2.0,-2.0 c 0.0,0.0 0.0,0.0 0.0,0.0 Z"
                android:fillColor="#FF000000"
                android:fillAlpha="0" />
        </group>
    </group>
</vector>

btn_checkbox_checked_to_unchecked_mtrl_animation.xml btn_checkbox_checked_to_unchecked_mtrl_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/btn_checkbox_checked_mtrl">
    <target
        android:name="icon_null"
        android:animation="@anim/btn_checkbox_to_unchecked_icon_null_animation" />
    <target
        android:name="check_path_merged"
        android:animation="@anim/btn_checkbox_to_unchecked_check_path_merged_animation" />
    <target
        android:name="box_inner_merged"
        android:animation="@anim/btn_checkbox_to_unchecked_box_inner_merged_animation" />
</animated-vector>

btn_checkbox_unchecked_mtrl.xml btn_checkbox_unchecked_mtrl.xml

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="btn_checkbox_unchecked"
    android:width="32dp"
    android:viewportWidth="48"
    android:height="32dp"
    android:viewportHeight="48"
    android:tint="@color/control_checkable_material">
    <group
        android:name="icon_null"
        android:translateX="24"
        android:translateY="24"
        android:scaleX="0.2"
        android:scaleY="0.2">
        <group
            android:name="check"
            android:scaleX="7.5"
            android:scaleY="7.5">
            <path
                android:name="box_outer_merged"
                android:pathData="M 7.0,-9.0 c 0.0,0.0 -14.0,0.0 -14.0,0.0 c -1.1044921875,0.0 -2.0,0.8955078125 -2.0,2.0 c 0.0,0.0 0.0,14.0 0.0,14.0 c 0.0,1.1044921875 0.8955078125,2.0 2.0,2.0 c 0.0,0.0 14.0,0.0 14.0,0.0 c 1.1044921875,0.0 2.0,-0.8955078125 2.0,-2.0 c 0.0,0.0 0.0,-14.0 0.0,-14.0 c 0.0,-1.1044921875 -0.8955078125,-2.0 -2.0,-2.0 c 0.0,0.0 0.0,0.0 0.0,0.0 Z M -2.0,5.00001525879 c 0.0,0.0 -1.4234161377,-1.40159606934 -1.4234161377,-1.40159606934 c 0.0,0.0 1.41409301758,-1.41409301758 1.41409301758,-1.41409301758 c 0.0,0.0 0.00932312011719,-0.0124053955078 0.00932312011719,-0.0124053955078 c 0.0,0.0 0.0234069824219,-0.0235137939453 0.0234069824219,-0.0235137939453 c 0.0,0.0 1.41409301758,1.41409301758 1.41409301758,1.41409301758 c 0.0,0.0 -1.4375,1.43751525879 -1.4375,1.43751525879 Z"
                android:fillColor="#FF000000"
                android:fillAlpha="0" />
        </group>
        <group
            android:name="box_dilate"
            android:scaleX="7.5"
            android:scaleY="7.5">
            <path
                android:name="box_inner_merged"
                android:pathData="M -7.0,-7.0 l 14.0,0.0 c 0.0,0.0 0.0,0.0 0.0,0.0 l 0.0,14.0 c 0.0,0.0 0.0,0.0 0.0,0.0 l -14.0,0.0 c 0.0,0.0 0.0,0.0 0.0,0.0 l 0.0,-14.0 c 0.0,0.0 0.0,0.0 0.0,0.0 Z M 7.0,-9.0 c 0.0,0.0 -14.0,0.0 -14.0,0.0 c -1.1044921875,0.0 -2.0,0.8955078125 -2.0,2.0 c 0.0,0.0 0.0,14.0 0.0,14.0 c 0.0,1.1044921875 0.8955078125,2.0 2.0,2.0 c 0.0,0.0 14.0,0.0 14.0,0.0 c 1.1044921875,0.0 2.0,-0.8955078125 2.0,-2.0 c 0.0,0.0 0.0,-14.0 0.0,-14.0 c 0.0,-1.1044921875 -0.8955078125,-2.0 -2.0,-2.0 c 0.0,0.0 0.0,0.0 0.0,0.0 Z"
                android:fillColor="#FF000000" />
        </group>
    </group>
</vector>

btn_checkbox_unchecked_to_checked_mtrl_animation.xml btn_checkbox_unchecked_to_checked_mtrl_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/btn_checkbox_unchecked_mtrl">
    <target
        android:name="icon_null"
        android:animation="@anim/btn_checkbox_to_checked_icon_null_animation" />
    <target
        android:name="box_outer_merged"
        android:animation="@anim/btn_checkbox_to_checked_box_outer_merged_animation" />
    <target
        android:name="box_inner_merged"
        android:animation="@anim/btn_checkbox_to_checked_box_inner_merged_animation" />
</animated-vector>
  1. copy this xml file into /res/color directory将此xml文件复制到/res/color目录中

control_checkable_material.xml control_checkable_material.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false"
        android:alpha="0.5"
        android:color="?attr/colorControlNormal" />
    <item android:state_checked="true"
        android:color="?attr/colorControlActivated" />
    <item android:color="?attr/colorControlNormal" />
</selector>
  1. now use radioButton in your project and change it's button attribute现在在您的项目中使用 radioButton 并更改它的按钮属性
<RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/btn_check_material_anim"
            android:text="radio1"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/btn_check_material_anim"
            android:text="radio2"/>
</RadioGroup>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM