Android UI 详解之单选(RadioButton)和复选(CheckBox)

一、

       1、 单选框,复选框是所有用户界面中最普遍的UI组件,android中二者继承了Button,因此他们都可以直接使用Button支持的各种属性和方法。他们和其他按钮的区别就是有额外属性android:checked

实现RadioButton和CheckBox有两种方法,

      2、通过setOnCheckedChangeListener监听器,去监听获得,但值得注意的是

RadioButton和CheckBox用的名字相同的setOnCheckedChangeListener 但是却不是一个,他们是不同包下的

,通过我们以前用过很多监听器,我们不难发现,我们set监听器名,参数就是new 监听器名 ,便于我们记忆。

<?xml version="1.0" encoding="utf-8"?>

Java 代码


package org.crazyit.ui;import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.TextView;public class CheckButtonTest extends Activity{RadioGroup rg;TextView show;CheckBox cb1;TextView tv;int sum = 0;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);// 获取界面上rg、show两个组件rg = (RadioGroup) findViewById(R.id.rg);show = (TextView) findViewById(R.id.show);tv = (TextView) findViewById(R.id.tv);cb1 = (CheckBox)findViewById(R.id.checkb1);// 为RadioGroup组件的OnCheck事件绑定事件监听器rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId){// 根据用户勾选的单选按钮来动态改变tip字符串的值String tip = checkedId == R.id.male ?"您的性别是男人": "您的性别是女人";// 修改show组件中的文本。show.setText(tip);}});cb1.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(cb1.isChecked())                  {                      tv.setText("你刚刚选择了"+ cb1.getText());                      sum++;                  }                  else                  {                      tv.setText("你刚刚取消了选择"+cb1.getText());                      sum--;                  }                                }});}}



我们遍历控件的方法

 

public class MainActivity extends Activity {private RadioGroup group;private Button btn;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);group = (RadioGroup) findViewById(R.id.radiogroup01);btn = (Button) findViewById(R.id.button01);btn.setOnClickListener(new OnClickListener() {public void onClick(View v) {int len = group.getChildCount();//获取总的长度String msg = "";for (int i = 0; i < len; i++) {  //遍历每一个radioButtonRadioButton radioButton = (RadioButton) group.getChildAt(i);if (radioButton.isChecked()) {msg = radioButton.getText().toString();break;}}Toast.makeText(MainActivity.this, msg, 1).show();}});}}

我们在实现一个下动态的创建CheckBox,

public class MainActivity extends Activity implements OnClickListener {private List CheckBoxs = new ArrayList();private Button btn;// private CheckBox checkBox;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// setContentView(R.layout.main);String[] CheckBoxText = new String[] { "地球", "亚洲", "中国", "上海", "杨浦","浦东" };// 动态加载布局LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);// 给CheckBox赋值for (int i = 0; i < CheckBoxText.length; i++) {// 获得checkBox.xml对象CheckBox checkBox = (CheckBox) getLayoutInflater().inflate(R.layout.checkbox, null);CheckBoxs.add(checkBox);        CheckBoxs.get(i).setText(CheckBoxText[i]);linearLayout.addView(checkBox, i);}setContentView(linearLayout);btn = (Button) findViewById(R.id.button01);btn.setOnClickListener(this);}public void onClick(View v) {String msg = "";for (CheckBox checkBox : CheckBoxs) {if (checkBox.isChecked()) {msg += checkBox.getText().toString() + "\n";}}if (msg.equals("")) {msg = "您还没有选择!";}new AlertDialog.Builder(this).setMessage(msg).setPositiveButton("关闭", null).show();}}

重点就是,先获一个模板checkbox对象,然后设置他的text值,在添加到布局文件中
CheckBox.xml

<?xml version="1.0" encoding="utf-8"?>

main.xml

    


   


   

更多相关文章

  1. 从Android界面开发谈起
  2. Android自定义进度条
  3. 详解 Android(安卓)的 Activity 组件
  4. Android自用-----Intent 介绍
  5. Android(安卓)Animation学习笔记
  6. Android深入理解WebView——上
  7. 详解 Android(安卓)的 Activity 组件
  8. android Intent机制详解
  9. Android加密之文件级加密

随机推荐

  1. 四、在ANDROID中调试程序
  2. [Android]开发环境配置(windows)-draft
  3. Use twitter4j oauth to post status in
  4. android中的progressbar
  5. android异步任务完成后再执行主线程任务
  6. android 中的 ViewPager+ Fragment
  7. android中处理图片成圆角
  8. Android 解析字符乱码解决
  9. Android 添加系统设置属性
  10. Android AsyncTask 源码解析