【问题标题】:How to select Multiple options from RadioGroup Buttons?如何从 RadioGroup 按钮中选择多个选项?
【发布时间】:2023-03-10 07:12:01
【问题描述】:

可以从 android 的 radiogroup 按钮中选择多个选项,请提供一些解决方案。 这是我在此代码中的代码,我只能从组中选择一个单选按钮,请为选择多个选项提供解决方案。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:id="@+id/text"
        android:text="@string/ChoiceText" />

    <RadioGroup 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:id="@+id/myRadioGroup"
        android:background="#abf234"
        android:checkedButton="@+id/sound" >

        <RadioButton 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/sound"
            android:text="@string/Sound" />

        <RadioButton 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/vibration"
            android:text="@string/Vibration" />

        <RadioButton 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/silent"
            android:text="@string/Silent" />

    </RadioGroup>

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/myRadioGroup"
        android:layout_marginTop="10dp"
        android:id="@+id/chooseBtn"
        android:text="@string/Choose" />

</RelativeLayout>

活动

public class MainActivity extends Activity {

    private RadioGroup radioGroup;
    private RadioButton sound, vibration, silent; 
    private Button button;
    private TextView textView;

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

        radioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);

        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // find which radio button is selected
                if(checkedId == R.id.silent) {
                    Toast.makeText(getApplicationContext(), "choice: Silent", 
                            Toast.LENGTH_SHORT).show();
                } else if(checkedId == R.id.sound) {
                    Toast.makeText(getApplicationContext(), "choice: Sound", 
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "choice: Vibration", 
                            Toast.LENGTH_SHORT).show();
                }
            }

        });

        sound = (RadioButton) findViewById(R.id.sound);
        vibration = (RadioButton) findViewById(R.id.vibration);
        silent = (RadioButton) findViewById(R.id.silent);
        textView = (TextView) findViewById(R.id.text);

        button = (Button)findViewById(R.id.chooseBtn);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                int selectedId = radioGroup.getCheckedRadioButtonId();

                // find which radioButton is checked by id
                if(selectedId == sound.getId()) {
                    textView.setText("You chose 'Sound' option");
                } else if(selectedId == vibration.getId()) {
                    textView.setText("You chose 'Vibration' option");
                } else {
                    textView.setText("You chose 'Silent' option");
                }   
            }
        });
    }

}

【问题讨论】:

  • 使用复选框而不是单选按钮进行多选..
  • 单选按钮设计用于在所有选项中选择选项。选择多个选项使用复选框
  • 只需删除单选组并使用单选按钮

标签: android android-radiogroup


【解决方案1】:

不要使用单选组,只需使用一些单选按钮集合。 RadioGroup 用于从单选按钮列表中选择一个。所以你的 xml 在更改后将如下所示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:id="@+id/text"
    android:text="@string/ChoiceText" />



    <RadioButton 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/sound"
        android:text="@string/Sound" />

    <RadioButton 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/vibration"
        android:text="@string/Vibration" />

    <RadioButton 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/silent"
        android:text="@string/Silent" />



<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/myRadioGroup"
    android:layout_marginTop="10dp"
    android:id="@+id/chooseBtn"
    android:text="@string/Choose" />

查看无线电组是否已删除。

【讨论】:

  • 根据上面@malli 的评论,最好使用复选框进行多选,而不是单选按钮。
【解决方案2】:

如果您想有多个条目,请使用复选框。 :)

http://developer.android.com/reference/android/widget/CheckBox.html

【讨论】:

    猜你喜欢
    • 2021-10-04
    • 2016-06-28
    • 2021-11-08
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 2010-10-06
    相关资源
    最近更新 更多