Android:设置默认选择芯片

时间:2019-08-12 16:01:58

标签: android xml material-design material-components-android android-chips

是否可以将选择芯片设置为默认选项?此外,如果只选择一个芯片,是否不能取消选中任何芯片?

2 个答案:

答案 0 :(得分:3)

您可以在ChipGroup组件中使用以下属性:

  • app:singleSelection="true"。这样,ChipGroup可以配置为仅允许一次使用来检查单个芯片
  • app:checkedChip定义默认选择

类似的东西:

        <com.google.android.material.chip.ChipGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:singleSelection="true"
            app:checkedChip="@id/ch2">

            <com.google.android.material.chip.Chip
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/ch1"
                android:text="M"/>

            <com.google.android.material.chip.Chip
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/ch2"
                android:text="F"/>

        </com.google.android.material.chip.ChipGroup>

答案 1 :(得分:1)

下面的示例也许可以帮助您使用正在谈论的单选按钮。 在您的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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"> 

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select your Subject ?"
        android:textStyle="bold"
        android:layout_marginLeft="10dp"
        android:textSize="20dp"/> 

    <!-- add RadioGroup which contain the many RadioButton-->
    <RadioGroup
        android:layout_marginTop="50dp"
        android:id="@+id/groupradio"
        android:layout_marginLeft="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"> 

        <!-- In RadioGroup create the 1 Radio Button-->
        <!-- like this we will add some more Radio Button-->
        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radia_id1"
            android:text="DBMS"
            android:textSize="20dp"/> 

        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radia_id2"
            android:text="C/C++ Programing"
            android:textSize="20dp"/> 

        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radia_id3"
            android:text="Data Structure"
            android:textSize="20dp"/> 

        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radia_id4"
            android:text="Algorithms"
            android:textSize="20dp"/> 
    </RadioGroup> 

    <!-- add button For Submit the Selected item-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:id="@+id/submit"
        android:textStyle="bold"
        android:textSize="20dp"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="180dp"
        /> 

    <!-- add clear button for clear the selected item-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear"
        android:id="@+id/clear"
        android:textSize="20dp"
        android:textStyle="bold"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="20dp"
        /> 

</RelativeLayout> 

然后在Java文件中编写以下代码。

package org.geeksforgeeks.navedmalik.radiobuttons; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 

    // Define the object for Radio Group, 
    // Submit and Clear buttons 
    private RadioGroup radioGroup; 
    Button submit, clear; 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 

        // Bind the components to their respective objects 
        // by assigning their IDs 
        // with the help of findViewById() method 
        submit = (Button)findViewById(R.id.submit); 
        clear = (Button)findViewById(R.id.clear); 
        radioGroup = (RadioGroup)findViewById(R.id.groupradio); 

        // Uncheck or reset the radio buttons initially 
        radioGroup.clearCheck(); 

        // Add the Listener to the RadioGroup 
        radioGroup.setOnCheckedChangeListener( 
            new RadioGroup 
                .OnCheckedChangeListener() { 
                    @Override

                    // The flow will come here when 
                    // any of the radio buttons in the radioGroup 
                    // has been clicked 

                    // Check which radio button has been clicked 
                    public void onCheckedChanged(RadioGroup group, 
                                                int checkedId) 
                    { 

                        // Get the selected Radio Button 
                        RadioButton 
                            radioButton 
                            = (RadioButton)group 
                                .findViewById(checkedId); 
                    } 
                }); 

        // Add the Listener to the Submit Button 
        submit.setOnClickListener(new View.OnClickListener() { 

            @Override
            public void onClick(View v) 
            { 

                // When submit button is clicked, 
                // Ge the Radio Button which is set 
                // If no Radio Button is set, -1 will be returned 
                int selectedId = radioGroup.getCheckedRadioButtonId(); 
                if (selectedId == -1) { 
                    Toast.makeText(MainActivity.this, 
                                "No answer has been selected", 
                                Toast.LENGTH_SHORT) 
                        .show(); 
                } 
                else { 

                    RadioButton radioButton 
                        = (RadioButton)radioGroup 
                            .findViewById(selectedId); 

                    // Now display the value of selected item 
                    // by the Toast message 
                    Toast.makeText(MainActivity.this, 
                                radioButton.getText(), 
                                Toast.LENGTH_SHORT) 
                        .show(); 
                } 
            } 
        }); 

        // Add the Listener to the Submit Button 
        clear.setOnClickListener(new View.OnClickListener() { 

            @Override
            public void onClick(View v) 
            { 

                // Clear RadioGroup 
                // i.e. reset all the Radio Buttons 
                radioGroup.clearCheck(); 
            } 
        }); 
    } 
}