RadioGroup組件能夠容納若干個RadioButton組件,每個RadioButton對應一個選項,利用RadioGroup相似於作單選題。java
RadioGroup能夠理解爲存放RadioButton的容器,他將多個RadioButton組織起來,造成一個組,而用戶在選擇時只能是組內的某一個RadioButton,因此用戶直接操做的對象是RadioButton組件。android
如下實例是首先選擇一個選項,按提交按鈕,會彈出一個消息框提示選擇信息。app
首先是佈局文件:ide
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="main.test_radiogroup.MainActivity" > <TextView android:id="@+id/TextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tv1" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/rb1" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rb2" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rb3" /> <RadioButton android:id="@+id/radio3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rb4"/> <RadioButton android:id="@+id/radio4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rb5"/> </RadioGroup> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn" /> </LinearLayout>
其次是strings.xml文件:佈局
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Test_RadioGroup</string> <string name="action_settings">Settings</string> <string name="tv1">請選出您最喜歡的一道菜:</string> <string name="rb1">鐵鍋蛋</string> <string name="rb2">蜜汁兩樣</string> <string name="rb3">瓷壇羊肉</string> <string name="rb4">肉絲帶底</string> <string name="rb5">活魚活吃</string> <string name="btn">提交</string> </resources>
再次是android源文件:測試
package main.test_radiogroup; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RadioButton; import android.widget.Toast; import android.os.Bundle; public class MainActivity extends ActionBarActivity implements OnClickListener{ private RadioButton rbtn1=null,rbtn2=null,rbtn3=null,rbtn4=null,rbtn5=null; private Button btn1=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rbtn1=(RadioButton)findViewById(R.id.radio0); rbtn2=(RadioButton)findViewById(R.id.radio1); rbtn3=(RadioButton)findViewById(R.id.radio2); rbtn4=(RadioButton)findViewById(R.id.radio3); rbtn5=(RadioButton)findViewById(R.id.radio4); btn1=(Button)findViewById(R.id.button1); btn1.setOnClickListener(MainActivity.this); } @Override public void onClick(View arg0) { String str=""; if(rbtn1.isChecked()) str=rbtn1.getText().toString(); else if(rbtn2.isChecked()) str=rbtn2.getText().toString(); else if(rbtn3.isChecked()) str=rbtn3.getText().toString(); else if(rbtn4.isChecked()) str=rbtn4.getText().toString(); else if(rbtn5.isChecked()) str=rbtn5.getText().toString(); else { } Toast.makeText(MainActivity.this,"您選擇的是"+str,Toast.LENGTH_LONG).show();//彈出選擇消息框 } }
最後是測試結果:this