回答

0

RadioButtons是与Button进行交互的不错风格。

首先,声明radioGroup中块上的活动XML文件

1)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <RadioGroup 
     android:id="@+id/radiogroup_choice" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <RadioButton 
      android:id="@+id/choice_yes" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/choice_yes" 
      android:checked="true" /> 

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

    </RadioGroup> 

</LinearLayout> 

2)然后,在你PreferenceActivity,活动或片段(无论您使用),以及之后您将内容用查看setContentView(R.layout.your_activity_layout);您可以通过以下操作实例化您的RadioGroup中:

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup_choice);   
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      switch(checkedId) { 
       case R.id.choice_yes: 
         // button yes was selected. do your work here 
        break; 
       case R.id.choice_no: 
         // button no was selected. do your work here 
        break; 
       default: 
        // default stuff 
        break; 

      } 
     } 
    }); 

此外,既然单选扩展Android的基本按钮,你还可以将一个onClickListener它,因此动作被触发时你按下按钮而不是更改yes和no之间的选项。

Regards,