2016-03-08 21 views
1

可以從Android的PLZ中的radiogroup按鈕中選擇多個選項給出一些解決方案。 這是我在這段代碼中的代碼,我只能從組中選擇一個單選按鈕,併爲選擇多個選擇提供解決方案。如何從RadioGrop按鈕中選擇多個選項?

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

} 
+1

複選框的使用,而不是對於多重選擇單選按鈕.. – malli

+0

單選按鈕設計,選擇在所有選項中選擇。選擇複選框使用複選框 –

+0

只是刪除收音機組並使用單選按鈕 –

回答

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" /> 

見無線電組被刪除後。

+0

根據上面的@ malli的評論,最好使用複選框進行多選,而不是單選按鈕。 –

相關問題