打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
单选按钮 RadioButton 的应用

原文 http://hualang.iteye.com/blog/964507

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

RadioButton的单选按钮;

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

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

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

 

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

例子:

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

RadioTest.java

 

Java代码  
  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.     /** Called when the activity is first created. */  
  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代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">哪个城市美女多?</string>  
  4.     <string name="app_name">单选按钮测试</string>  
  5.     <string name="radiobutton1">杭州</string>  
  6.     <string name="radiobutton2">成都</string>  
  7.     <string name="radiobutton3">重庆</string>  
  8.     <string name="radiobutton4">苏州</string>  
  9. </resources>  

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

 

Java代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns: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>  

 运行结果如下:



 假如我们选择杭州,会提示错误的Toast



 再次选中成都后,会提示答案正确



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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Android从零开始(3)(常用控件+下拉框视图)(新)
Android RadioButton控件
Android软件开发之盘点常用系统控件界面大合集(三)
一步一步android(8):关于界面控件的学习2【edittext、radiogroup、checkbox】
Android学习笔记(六):xml和widget
5.2.2 Fragment实例精讲
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服