原文 http://hualang./blog/964507 單選按鈕RadioButton在Android平臺上也應(yīng)用的非常多,比如一些選擇項的時候,會用到單選按鈕,實現(xiàn)單選按鈕由兩部分組成,也就是RadioButton和RadioGroup配合使用
RadioButton的單選按鈕;
RadioGroup是單選組合框,用于將RadioButton框起來;
在沒有RadioGroup的情況下,RadioButton可以全部都選中;
當多個RadioButton被RadioGroup包含的情況下,RadioButton只可以選擇一個;
注意:單選按鈕的事件監(jiān)聽用setOnCheckedChangeListener來對單選按鈕進行監(jiān)聽
例子:
一道選擇題,選擇哪個城市美女最多,當然,這個就是為了測試
RadioTest.java
- package org.loulijun.radio;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.TextView;
- import android.widget.Toast;
-
- public class RadioTest extends Activity {
- /** Called when the activity is first created. */
- TextView textview;
- RadioGroup radiogroup;
- RadioButton radio1,radio2,radio3,radio4;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- textview=(TextView)findViewById(R.id.textview1);
- radiogroup=(RadioGroup)findViewById(R.id.radiogroup1);
- radio1=(RadioButton)findViewById(R.id.radiobutton1);
- radio2=(RadioButton)findViewById(R.id.radiobutton2);
- radio3=(RadioButton)findViewById(R.id.radiobutton3);
- radio4=(RadioButton)findViewById(R.id.radiobutton4);
-
- radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
-
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- // TODO Auto-generated method stub
- if(checkedId==radio2.getId())
- {
- DisplayToast("正確答案:"+radio2.getText()+",恭喜你,回答正確!");
- }else
- {
- DisplayToast("請注意,回答錯誤!");
- }
- }
- });
- }
- public void DisplayToast(String str)
- {
- Toast toast=Toast.makeText(this, str, Toast.LENGTH_LONG);
- toast.setGravity(Gravity.TOP,0,220);
- toast.show();
- }
- }
strings.xml文件
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">哪個城市美女多?</string>
- <string name="app_name">單選按鈕測試</string>
- <string name="radiobutton1">杭州</string>
- <string name="radiobutton2">成都</string>
- <string name="radiobutton3">重慶</string>
- <string name="radiobutton4">蘇州</string>
- </resources>
main.xml文件:注意,這里面,4個RadioButton包含在RadioGroup中
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas./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/hello"
- android:id="@+id/textview1"
- />
- <RadioGroup
- android:id="@+id/radiogroup1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:layout_x="3px"
- >
- <RadioButton
- android:id="@+id/radiobutton1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radiobutton1"
- />
- <RadioButton
- android:id="@+id/radiobutton2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radiobutton2"
- />
- <RadioButton
- android:id="@+id/radiobutton3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radiobutton3"
- />
- <RadioButton
- android:id="@+id/radiobutton4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radiobutton4"
- />
- </RadioGroup>
- </LinearLayout>
運行結(jié)果如下:
假如我們選擇杭州,會提示錯誤的Toast
再次選中成都后,會提示答案正確
這里就可以看到,單選按鈕的使用效果,如果只是使用RadioButton的話,把配置文件中RadioGroup去掉,當然,要重新為每個單選按鈕設(shè)置監(jiān)聽,這樣,這個RadioButton就跟Button沒有什么區(qū)別了,我們可以選中多個,所以要注意,單選按鈕要和RadioGroup一起使用,才能夠?qū)崿F(xiàn)單選的功能
|