Как выбрать несколько вариантов из кнопок RadioGrop?

можно выбрать несколько вариантов с помощью кнопок радиогруппы в Android, пожалуйста, дайте какое-нибудь решение. этот мой код в этом коде я могу выбрать только один переключатель из группы, пожалуйста, дайте решение для выбора более одного выбора.

<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");
                }   
            }
        });
    }

}

person praveen s    schedule 08.03.2016    source источник
comment
используйте флажки вместо переключателей для множественного выбора.   -  person malli    schedule 08.03.2016
comment
Радиокнопка предназначена для выбора опции среди всех опций. чтобы выбрать несколько вариантов, используйте флажок   -  person Khizar Hayat    schedule 08.03.2016
comment
просто удалите радиогруппу и используйте радиокнопки   -  person Anup Dasari    schedule 08.03.2016


Ответы (2)


Вместо использования группы переключателей просто используйте набор переключателей. 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" />

Увидите, что радиогруппа удалена.

person Hari Krishnan    schedule 08.03.2016
comment
Согласно комментарию @malli выше, лучше использовать флажки для множественного выбора, а не радиокнопку. - person AL.; 08.03.2016

Пожалуйста, используйте флажок, если вы хотите иметь несколько записей. :)

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

person shibapoo    schedule 08.03.2016