Android---RadioButton(单选按钮)详解

来源:互联网 发布:淘宝台服战网点 编辑:程序博客网 时间:2024/04/30 17:39
一: 单选按钮RadioButton在Android平台上也应用的非常多,比如一些选择项的时候,会用到单选按钮,实现单选按钮由两部分组成,也就是RadioButton和RadioGroup配合使用

RadioButton的单选按钮;

RadioGroup是单选组合框,用于将RadioButton框起来;

在没有RadioGroup的情况下,RadioButton可以全部都选中;

当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个;

注意:单选按钮的事件监听用setOnCheckedChangeListener来对单选按钮进行监听

例子:一道选择题,选择哪个城市美女最多,当然,这个就是为了测试

Java代码
  1. package org.loulijun.radio;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Gravity;
  5. import android.widget.RadioButton;
  6. import android.widget.RadioGroup;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9. public class RadioTestextends Activity {
  10. /** Called when the activity is first created. */
  11. TextView textview;
  12. RadioGroup radiogroup;
  13. RadioButton radio1,radio2,radio3,radio4;
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. textview=(TextView)findViewById(R.id.textview1);
  19. radiogroup=(RadioGroup)findViewById(R.id.radiogroup1);
  20. radio1=(RadioButton)findViewById(R.id.radiobutton1);
  21. radio2=(RadioButton)findViewById(R.id.radiobutton2);
  22. radio3=(RadioButton)findViewById(R.id.radiobutton3);
  23. radio4=(RadioButton)findViewById(R.id.radiobutton4);
  24. radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  25. @Override
  26. public void onCheckedChanged(RadioGroup group,int checkedId) {
  27. // TODO Auto-generated method stub
  28. if(checkedId==radio2.getId())
  29. {
  30. DisplayToast("正确答案:"+radio2.getText()+",恭喜你,回答正确!");
  31. }else
  32. {
  33. DisplayToast("请注意,回答错误!");
  34. }
  35. }
  36. });
  37. }
  38. public void DisplayToast(String str)
  39. {
  40. Toast toast=Toast.makeText(this, str, Toast.LENGTH_LONG);
  41. toast.setGravity(Gravity.TOP,0,220);
  42. toast.show();
  43. }
  44. }

strings.xml文件

Xml代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="hello">哪个城市美女多?</string>
  4. <stringname="app_name">单选按钮测试</string>
  5. <stringname="radiobutton1">杭州</string>
  6. <stringname="radiobutton2">成都</string>
  7. <stringname="radiobutton3">重庆</string>
  8. <stringname="radiobutton4">苏州</string>
  9. </resources>

main.xml文件:注意,这里面,4个RadioButton包含在RadioGroup中

Xml代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. android:id="@+id/textview1"
  12. />
  13. <RadioGroup
  14. android:id="@+id/radiogroup1"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:orientation="vertical"
  18. android:layout_x="3px"
  19. >
  20. <RadioButton
  21. android:id="@+id/radiobutton1"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="@string/radiobutton1"
  25. />
  26. <RadioButton
  27. android:id="@+id/radiobutton2"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="@string/radiobutton2"
  31. />
  32. <RadioButton
  33. android:id="@+id/radiobutton3"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:text="@string/radiobutton3"
  37. />
  38. <RadioButton
  39. android:id="@+id/radiobutton4"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:text="@string/radiobutton4"
  43. />
  44. </RadioGroup>
  45. </LinearLayout>

运行结果如下:

二:Android 自定义RadioButton的样式(和上面关系不大

我们知道Android控件里的button,listview可以用xml的样式自定义成自己希望的漂亮样式。

最近用到RadioButton,利用xml修改android:background="@drawable/button_drawable",其中button_drawable为自己定义的.xml文件(res/drawable文件下),但是不成功,到网上查找,也没有正确的说法,我就开始自己尝试,最后做好了。

其实方法很简单,同样在res/drawable新建radiobutton.xml如下

Xml代码
  1. <selectorxmlns:android="http://schemas.android.com/apk/res/android">
  2. <item
  3. android:state_enabled="true"
  4. android:state_checked="true"
  5. android:drawable="@drawable/check"/>
  6. <item
  7. android:state_enabled="true"
  8. android:state_checked="false"
  9. android:drawable="@drawable/checknull"/>
  10. </selector>

check和checknull分别为选中和位选中的图片。
1然后在你的布局文件中,RadioButton 布局
1设置android:button = "@drawable/radiobutton",就可以了!

前后图片对比如下:

radio前radio后

原创粉丝点击