雾山的Anrdoid学习笔记---CheckBox,RadioGroup&RadioButton .

来源:互联网 发布:mysql 双主 英文描述 编辑:程序博客网 时间:2024/06/05 09:06

CheckBox是Android提供的多选按钮控件,它只有true和false两种状态。在xml中默认为false(未选中),也可以这样设置

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. android:checked="true"  

 这样的话,就默认选中了。


RadioButton为单选按钮。一个RadioGroup可以包含多个RadioButton。

RadioButton和RadioGroup的关系:
1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器
2、每个RadioGroup中的RadioButton同时只能有一个被选中
3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中
4、大部分场合下,一个RadioGroup中至少有2个RadioButton
5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置




RadioButton和CheckBox的区别:
1、单个RadioButton在选中后,通过点击无法变为未选中
    单个CheckBox在选中后,通过点击可以变为未选中

2、一组RadioButton,只能同时选中一个
     一组CheckBox,能同时选中多个

3、RadioButton在大部分UI框架中默认都以圆形表示
     CheckBox在大部分UI框架中默认都以矩形表示


下面看代码:

开发步骤: 
1,新建一个Android应用程序 
2,在布局文件中创建两个TextView控件标签,一个RadioGroup控件和四个CheckBox控件,并为其设置属性和值 
3,在Activity中,声明所有控件变量并根据id获得控件对象 ,设置RadioButton和CheckBox的监听器,
4,当选中all按钮的时候,要求所有的多选按钮都被选中

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.tangbc.choosedemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.widget.CheckBox;  
  7. import android.widget.CompoundButton;  
  8. import android.widget.CompoundButton.OnCheckedChangeListener;  
  9. import android.widget.RadioButton;  
  10. import android.widget.RadioGroup;  
  11. import android.widget.Toast;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     private RadioGroup radioGroup;  
  15.     private RadioButton boyButton;  
  16.     private RadioButton girlButton;  
  17.     private CheckBox wowCB;  
  18.     private CheckBox lolCB;  
  19.     private CheckBox dotaCB;  
  20.     private CheckBox allCB;  
  21.   
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.activity_main);  
  26.           
  27.         //为所有控件绑定id   
  28.         radioGroup = (RadioGroup) findViewById(R.id.radioGroup);  
  29.         boyButton = (RadioButton) findViewById(R.id.boyButton);  
  30.         girlButton = (RadioButton) findViewById(R.id.girlButton);  
  31.   
  32.         wowCB = (CheckBox) findViewById(R.id.wowCheckBox);  
  33.         lolCB = (CheckBox) findViewById(R.id.lolCheckBox);  
  34.         dotaCB = (CheckBox) findViewById(R.id.dotaCheckBox);  
  35.         allCB = (CheckBox) findViewById(R.id.allCheckBox);  
  36.           
  37.          //绑定监听器   
  38.         radioGroup.setOnCheckedChangeListener(new RadioGroupListener());  
  39.         wowCB.setOnCheckedChangeListener(new CheckBoxListener());  
  40.         lolCB.setOnCheckedChangeListener(new CheckBoxListener());  
  41.         dotaCB.setOnCheckedChangeListener(new CheckBoxListener());  
  42.         allCB.setOnCheckedChangeListener(new CheckBoxListener());  
  43.     }  
  44.   
  45.     // 导入android.widget.CompoundButton.OnCheckedChangeListener  
  46.     class CheckBoxListener implements OnCheckedChangeListener {  
  47.   
  48.         @Override  
  49.         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  50.             if (buttonView.getId() == wowCB.getId()) {  
  51.                 if (isChecked == true) {  
  52.                     //获取点击按钮的text文本   
  53.                     String text = buttonView.getText().toString();  
  54.                     System.out.println(text );  
  55.                 } else {  
  56.                     System.out.println("cancelWOW");  
  57.                 }  
  58.             } else if (buttonView.getId() == lolCB.getId()) {  
  59.                 if (isChecked == true) {  
  60.                     String text = buttonView.getText().toString();  
  61.                     System.out.println(text );  
  62.                 } else {  
  63.                     System.out.println("cancelLOL");  
  64.                 }  
  65.             } else if (buttonView.getId() == dotaCB.getId()) {  
  66.                 if (isChecked == true) {  
  67.                     String text = buttonView.getText().toString();  
  68.                     System.out.println(text );  
  69.                 } else {  
  70.                     System.out.println("cancelDota");  
  71.                 }  
  72.             } else if (buttonView.getId() == allCB.getId()) {  
  73.                 if (isChecked == true) {        //all按钮为选中状态后,其他的按钮都被选中  
  74.                     System.out.println("all");  
  75.                     wowCB.setChecked(true);  
  76.                     lolCB.setChecked(true);  
  77.                     dotaCB.setChecked(true);  
  78.                 } else {  
  79.                     String text = buttonView.getText().toString();  
  80.                     System.out.println(text );  
  81.                 }  
  82.             } else {  
  83.                 System.out.println("default");  
  84.             }  
  85.         }  
  86.   
  87.     }  
  88.   
  89.     // 导入android.widget.RadioGroup.OnCheckedChangeListener  
  90.     class RadioGroupListener implements  
  91.             android.widget.RadioGroup.OnCheckedChangeListener {  
  92.   
  93.         @Override  
  94.         public void onCheckedChanged(RadioGroup group, int checkedId) {  
  95.             if (checkedId == boyButton.getId()) {  
  96.                 System.out.println("boy");  
  97.                 Toast.makeText(MainActivity.this"你选的是boy", Toast.LENGTH_SHORT).show();  
  98.             } else if (checkedId == girlButton.getId()) {  
  99.                 System.out.println("girl");  
  100.                 Toast.makeText(MainActivity.this"你选的是girl", Toast.LENGTH_SHORT).show();  
  101.             }  
  102.         }  
  103.   
  104.     }  
  105.   
  106.     @Override  
  107.     public boolean onCreateOptionsMenu(Menu menu) {  
  108.         // Inflate the menu; this adds items to the action bar if it is present.  
  109.         getMenuInflater().inflate(R.menu.main, menu);  
  110.         return true;  
  111.     }  
  112.   
  113. }  

XML文件


[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <TextView  
  12.         android:id="@+id/sexTextView"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="你是男生还是女生" />  
  16.   
  17.     <RadioGroup  
  18.         android:id="@+id/radioGroup"  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_below="@id/sexTextView" >  
  22.   
  23.         <RadioButton  
  24.             android:id="@+id/boyButton"  
  25.             android:layout_width="match_parent"  
  26.             android:layout_height="wrap_content"  
  27.             android:text="boy" />  
  28.   
  29.         <RadioButton  
  30.             android:id="@+id/girlButton"  
  31.             android:layout_width="match_parent"  
  32.             android:layout_height="wrap_content"  
  33.             android:text="girl" />  
  34.     </RadioGroup>  
  35.   
  36.     <TextView  
  37.         android:id="@+id/gameTextView"  
  38.         android:layout_width="match_parent"  
  39.         android:layout_height="wrap_content"  
  40.         android:layout_below="@id/radioGroup"  
  41.         android:text="你喜欢玩的游戏" />  
  42.   
  43.     <CheckBox  
  44.         android:id="@+id/wowCheckBox"  
  45.         android:layout_width="match_parent"  
  46.         android:layout_height="wrap_content"  
  47.         android:layout_below="@id/gameTextView"  
  48.         android:checked="false"  
  49.         android:text="WOW" />  
  50.   
  51.     <CheckBox  
  52.         android:id="@+id/lolCheckBox"  
  53.         android:layout_width="match_parent"  
  54.         android:layout_height="wrap_content"  
  55.         android:layout_below="@id/wowCheckBox"  
  56.          android:checked="false"  
  57.         android:text="LOL" />  
  58.   
  59.     <CheckBox  
  60.         android:id="@+id/dotaCheckBox"  
  61.         android:layout_width="match_parent"  
  62.         android:layout_height="wrap_content"  
  63.         android:layout_below="@id/lolCheckBox"  
  64.         android:text="Dota" />  
  65.   
  66.     <CheckBox  
  67.         android:id="@+id/allCheckBox"  
  68.         android:layout_width="match_parent"  
  69.         android:layout_height="wrap_content"  
  70.         android:layout_below="@id/dotaCheckBox"  
  71.         android:text="all" />  
  72.   
  73. </RelativeLayout>  

点我下载源码
0 0
原创粉丝点击