单选按钮RadioButton在Android平台上也应用的非常多,比如一些选择项的时候,会用到单选按钮,实现单选按钮由两部分组成,也就是RadioButton和RadioGroup配合使用

RadioButton的单选按钮;

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

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

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

 

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

例子:

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

RadioTest.java

 

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

 

 strings.xml文件

 

Java代码   单选按钮RadioButton的使用" style="border:0px;list-style:none;" width="0" height="0">
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3.     "hello">哪个城市美女多?  
  4.     "app_name">单选按钮测试  
  5.     "radiobutton1">杭州  
  6.     "radiobutton2">成都  
  7.     "radiobutton3">重庆  
  8.     "radiobutton4">苏州  
  9.   

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

 

Java代码   单选按钮RadioButton的使用" style="border:0px;list-style:none;" width="0" height="0">
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. "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.     android:layout_width="fill_parent"   
  8.     android:layout_height="wrap_content"   
  9.     android:text="@string/hello"  
  10.     android:id="@+id/textview1"  
  11.     />  
  12.     
  13.         android:id="@+id/radiogroup1"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:orientation="vertical"  
  17.         android:layout_x="3px"  
  18.     >  
  19.         
  20.             android:id="@+id/radiobutton1"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content"  
  23.             android:text="@string/radiobutton1"  
  24.         />  
  25.         
  26.             android:id="@+id/radiobutton2"  
  27.             android:layout_width="wrap_content"  
  28.             android:layout_height="wrap_content"  
  29.             android:text="@string/radiobutton2"  
  30.         />  
  31.         
  32.             android:id="@+id/radiobutton3"  
  33.             android:layout_width="wrap_content"  
  34.             android:layout_height="wrap_content"  
  35.             android:text="@string/radiobutton3"  
  36.         />  
  37.         
  38.             android:id="@+id/radiobutton4"  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="wrap_content"  
  41.             android:text="@string/radiobutton4"  
  42.         />  
  43.       
  44.   

 运行结果如下:


单选按钮RadioButton的使用" style="border:0px;list-style:none;" width="0" height="0">
 假如我们选择杭州,会提示错误的Toast


单选按钮RadioButton的使用" style="border:0px;list-style:none;" width="0" height="0">
 再次选中成都后,会提示答案正确


单选按钮RadioButton的使用" style="border:0px;list-style:none;" width="0" height="0">
 这 里就可以看到,单选按钮的使用效果,如果只是使用RadioButton的话,把配置文件中RadioGroup去掉,当然,要重新为每个单选按钮设置监 听,这样,这个RadioButton就跟Button没有什么区别了,我们可以选中多个,所以要注意,单选按钮要和RadioGroup一起使用,才能 够实现单选的功能


二:修改RadioButton样式

可能你觉得RadioButton的样式不好看,没关系,你可以自己设置喜欢的图片显示,方法如下:

1)在drawable文件夹下增加文档myradiobutton.xml,描述button在各个状态下得图片:


       
     

2)然后在布局文件中修改Radiobutton属性(android:button):

     android:id= "@+id/rb_5"               android:layout_width= "120px"      android:layout_height= "85px" android:button="@null"
     android:button= "@drawable/myradiobutton" />

更多相关文章

  1. Android(安卓)带checkbox的listView 实现多选,全选,反选
  2. Android环境搭建_
  3. Android(安卓)动态添加按钮,并获取id,添加事件
  4. Android常用控件(按钮、选择框、日期时间控件)
  5. Android(安卓)Studio如何导入第三方工程包
  6. (ios实现)用c/c++混合编程方式为ios/android实现一个自绘日期选
  7. Android(安卓)ADT下载安装
  8. #Android学习#android:src=""的用法
  9. Android(安卓)Studio TV开发教程(二)管理电视控制器

随机推荐

  1. Android(安卓)中文API:如何安全的为APK进
  2. android resouce list
  3. Android(安卓)TabHost与FragmentActivity
  4. Android(安卓)检测网络是否可用
  5. AndroidManifest.xml文件详解(uses-config
  6. android 华为Mate 10 spinner 点击区域bu
  7. Android4.0中修改挂断键(ENDCALL)的默认行
  8. Android(安卓)利用handler传递数据
  9. android 显示 网络图片
  10. Android/Java仿微信按时间长短分类显示时