//示例RadioButton,通常都和RadioGroup組合一塊兒用 public class MainActivity extends Activity { private RadioGroup group; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); group=(RadioGroup) this.findViewById(R.id.group); //設置組的監聽事件: OnCheckedChangeListener group.setOnCheckedChangeListener(new OnCheckedChangeListener() { //在組中獲取按鈕點擊後的事件內容 @Override public void onCheckedChanged(RadioGroup arg0, int arg1) { RadioButton radiobutton1=(RadioButton) findViewById(arg1); Toast.makeText(MainActivity.this, "勾選了:" +radiobutton1.getText(), Toast.LENGTH_SHORT).show(); } }); } //注意:這個是在外面,組外獲取組內按鈕的事件內容,通常比較經常使用的是組外獲取 public void guess(View view){ int id=group.getCheckedRadioButtonId(); RadioButton radiobutton2=(RadioButton) findViewById(id); Toast.makeText(MainActivity.this, "勾選了:" +radiobutton2.getText(), Toast.LENGTH_SHORT).show(); } } //佈局 <LinearLayout 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:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <RadioGroup android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/stone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="石頭" /> <RadioButton android:id="@+id/sheet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="布" /> <RadioButton android:id="@+id/scissor" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="剪刀" /> </RadioGroup> <Button android:id="@+id/sumbit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="出拳" android:onClick="guess" /> </LinearLayout>