android-UI组件实例大全(四)------单选按钮(RadioButton)和复选框(CheckBox)

在我们的android中,单选按钮与复选框都是Button的子类,所以继承了Button的各种属性,而且还多了一个可选中的功能


一.单选按钮:RadioButton

为什么叫单选按钮呢,因为只能选中一个,所以需要把单选按钮放到我们的按钮组RadioGroup中,从而实现单选功能呢


代码:

布局代码:

[html]   view plain copy print ?
  1. <span style="font-family:Comic Sans MS;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/LinearLayout1"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="horizontal"  
  7.     tools:context=".MainActivity" >  
  8.   
  9.     <TextView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="请选择性别"  
  13.         android:textSize="23dp"  
  14.         />  
  15.       
  16.     <RadioGroup  
  17.         android:id="@+id/radioGroup"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:orientation="horizontal">  
  21.           
  22.         <RadioButton  
  23.             android:id="@+id/btnMan"  
  24.             android:layout_width="wrap_content"  
  25.             android:layout_height="wrap_content"  
  26.             android:text="男"  
  27.             android:checked="true"/>  
  28.           
  29.         <RadioButton  
  30.             android:id="@+id/btnWoman"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:text="女"/>  
  34.     </RadioGroup>  
  35.       
  36.     <Button  
  37.         android:id="@+id/btnpost"  
  38.         android:layout_width="wrap_content"  
  39.         android:layout_height="wrap_content"  
  40.         android:text="提交"   
  41.         />  
  42.   
  43. </LinearLayout></span>  


运行截图:


代码解释:

这里的话我们定义了一个TextView,两个radioButton,一个RadioGroup,一个按钮

checked:这个设置单选按钮时否被选中

在RadioGroup中的orientation:"horizontal"设置按钮组中的按钮按照水平方向进行摆放



接着的话我们通过两种方式获得选中按钮的值

1)单选按钮组的值发生改变时获取,即为RadioGroup设置一个事件监听器:setOnCheckChangeListener

代码:

[java]   view plain copy print ?
  1. <span style="font-family:Comic Sans MS;">radgroup = (RadioGroup) findViewById(R.id.radioGroup);  
  2.         //第一种获得单选按钮值的方法  
  3.         //为radioGroup设置一个监听器:setOnCheckedChanged()  
  4.         radgroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  5.               
  6.             @Override  
  7.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  8.                 RadioButton radbtn = (RadioButton) findViewById(checkedId);  
  9.                 Toast.makeText(getApplicationContext(), "按钮组值发生改变,你选了"+radbtn.getText(), Toast.LENGTH_LONG).show();  
  10.             }  
  11.         });</span>  


运行截图:



代码分析:

根据按钮组选中值的改变而触发时间,此时onCheckedChanged()方法中的checkedId就是当前选中按钮的id值,直接调用getText方法即可获得对应的值

这里的话要注意一点哦!!!!

按钮组中的单选按钮都要给一个id值哦,不然的话单选功能会失效的

,如图,一定要记住哦!!!!




2.单击其他按钮时获取选中的单选按钮的值

代码:

[java]   view plain copy print ?
  1. <span style="font-family:Comic Sans MS;">btnchange= (Button) findViewById(R.id.btnpost);  
  2.         radgroup = (RadioGroup) findViewById(R.id.radioGroup);  
  3.         //第一种获得单选按钮值的方法  
  4.         //为radioGroup设置一个监听器:setOnCheckedChanged()  
  5.           
  6.         btnchange.setOnClickListener(new OnClickListener() {  
  7.               
  8.             @Override  
  9.             public void onClick(View v) {  
  10.                 // TODO Auto-generated method stub  
  11.                 for(int i = 0;i < radgroup.getChildCount();i++)  
  12.                 {  
  13.                     RadioButton rd = (RadioButton) radgroup.getChildAt(i);  
  14.                       
  15.                     if(rd.isChecked())  
  16.                     {  
  17.                         Toast.makeText(getApplicationContext(), "点击提交按钮,获取你选择的是:"+rd.getText(), Toast.LENGTH_LONG).show();  
  18.                         break;  
  19.                     }  
  20.                 }  
  21.             }  
  22.         });</span>  


运行截图:


代码解释:

这里我们为提交按钮设置了一个setOnClickListener事件监听器,每次点击的话遍历一次RadioGroup判断哪个按钮呗选中

.getChildCont( )获得按钮组中的单选按钮的数目

.getChinldAt(i):根据索引值获取我们的单选按钮

isChecked:判断按钮是否选中





二.CheckBox复选框

相比于RadioButton,复选框获取值的话就有点麻烦了,因为用户可以多选,所以必须为每个复选框都设置一个setOnCheckedChangeListener

代码:

[java]   view plain copy print ?
  1. <span style="font-family:Comic Sans MS;">private Button btnchange;  
  2.     private CheckBox cboxWhite;  
  3.     private CheckBox cboxBlack;  
  4.     private CheckBox cboxGreen;  
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.           
  8.           
  9.           
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.           
  13.           
  14.         btnchange = (Button) findViewById(R.id.btnpost);  
  15.   
  16.         cboxWhite = (CheckBox) findViewById(R.id.cboxwhite);  
  17.         cboxBlack = (CheckBox) findViewById(R.id.cboxblack);  
  18.         cboxGreen = (CheckBox) findViewById(R.id.cboxgreen);  
  19.           
  20.           
  21.         //为三个复选框按钮都添加一个setOnCheckedChangeListener事件监听器  
  22.         cboxBlack.setOnCheckedChangeListener(checkBox_Listener);  
  23.         cboxWhite.setOnCheckedChangeListener(checkBox_Listener);  
  24.         cboxGreen.setOnCheckedChangeListener(checkBox_Listener);  
  25.           
  26.           
  27.           
  28.         //为提交按钮添加一个setOnClickListener  
  29.         btnchange.setOnClickListener(new OnClickListener() {  
  30.               
  31.             @Override  
  32.             public void onClick(View v) {  
  33.                 // TODO Auto-generated method stub  
  34.                 String choose = "";  
  35.                 if(cboxBlack.isChecked())  
  36.                     choose += cboxBlack.getText().toString()+" ";  
  37.                 if(cboxWhite.isChecked())  
  38.                     choose += cboxWhite.getText().toString()+" ";  
  39.                 if(cboxGreen.isChecked())  
  40.                     choose += cboxGreen.getText().toString()+" ";  
  41.                   
  42.                 Toast.makeText(MainActivity.this, choose, Toast.LENGTH_LONG).show();  
  43.             }  
  44.         });  
  45.           
  46.           
  47.     }  
  48.       
  49.     private OnCheckedChangeListener checkBox_Listener = new OnCheckedChangeListener() {  
  50.           
  51.   
  52.         @Override  
  53.         public void onCheckedChanged(CompoundButton buttonView,  
  54.                 boolean isChecked) {  
  55.             // TODO Auto-generated method stub  
  56.             if(isChecked);  
  57.         }  
  58.     };</span>  

运行截图:


代码解释:

这里提示一点,就是我们的findViewByID要在布局加载进去之后才使用,不然会报空指针异常,笔者就曾经在此纠结N久

这里的话我们自定义一个OnCheckedChangeListener的对象,然后为每个复选框添加这个对象作为监听器的参数

最后为Btnpost设置一个setOnclickListener,通过ischecked依次判断是否选中,最后使用toast输出选中的信息

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值