RadioButton(單選按鈕)在Android開發中應用的非常廣泛,比如一些選擇項的時候,會用到單選按鈕。它是一種單個圓形單選框雙狀態的按鈕,可以選擇或不選擇。在RadioButton沒有被選中時,用戶能夠按下或點擊來選中它。但是,與復選框相反,用戶一旦選中就不能夠取消選中。
實現RadioButton由兩部分組成,也就是RadioButton和RadioGroup配合使用.RadioGroup是單選組合框,可以容納多個RadioButton的容器.在沒有RadioGroup的情況下,RadioButton可以全部都選中;當多個RadioButton被RadioGroup包含的情況下,RadioButton只可以選擇一個。並用setOnCheckedChangeListener來對單選按鈕進行監聽
下面的具體的例子:
MainActivity.java
- package com.android.radiobutton;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.RadioGroup;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- //聲明RadioGroup
- RadioGroup raGroup1,raGroup2;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //通過findViewById獲得RadioGroup對象
- raGroup1=(RadioGroup)findViewById(R.id.radioGroup1);
- //添加事件監聽器
- raGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- // TODO Auto-generated method stub
- if(checkedId==R.id.radioBtn1){
- Toast.makeText(MainActivity.this, "你來自廣東省", Toast.LENGTH_LONG).show();
- }
- else if(checkedId==R.id.radioBtn2){
- Toast.makeText(MainActivity.this, "你來自廣西省", Toast.LENGTH_LONG).show();
- }
- else{
- Toast.makeText(MainActivity.this, "你來自湖南省", Toast.LENGTH_LONG).show();
- }
- }
- });
- raGroup2=(RadioGroup)findViewById(R.id.radioGroup2);
- raGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- // TODO Auto-generated method stub
- if(checkedId==R.id.radioBtn4){
- Toast.makeText(MainActivity.this, "你的性別是男", Toast.LENGTH_LONG).show();
- }
- else {
- Toast.makeText(MainActivity.this, "你的性別是女", Toast.LENGTH_LONG).show();
- }
- }
- });
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello1"
- />
- <RadioGroup
- android:id="@+id/radioGroup1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- >
- <RadioButton
- android:id="@+id/radioBtn1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radioBtn1"
- />
- <RadioButton
- android:id="@+id/radioBtn2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radioBtn2"
- />
- <RadioButton
- android:id="@+id/radioBtn3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radioBtn3"
- />
- </RadioGroup>
- <!-- 在兩個RadioGroup之間畫條橫線 -->
- <View
- android:layout_width="match_parent"
- android:layout_height="1dp"
- android:background="#ffffff"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello2"
- />
- <RadioGroup
- android:id="@+id/radioGroup2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- >
- <RadioButton
- android:id="@+id/radioBtn4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radioBtn4"
- android:textColor="#ffffff"
- />
- <RadioButton
- android:id="@+id/radioBtn5"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radioBtn5"
- />
- </RadioGroup>
- </LinearLayout>
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello1">你來自哪個省</string>
- <string name="hello2">你的性別是</string>
- <string name="app_name">單選按鈕測試</string>
- <string name="radioBtn1">廣東</string>
- <string name="radioBtn2">廣西</string>
- <string name="radioBtn3">湖南</string>
- <string name="radioBtn4">男</string>
- <string name="radioBtn5">女</string>
- </resources>
效果圖:
650) this.width=650;" height=120>
RadioButton的另一種效果:
650) this.width=650;" height=120>