源码下载:点击打开链接



(一)单选按钮RadioButton

单选按钮RadioButton,是仅可以选择一个选项的控件,继承自android.widget.CompoundButton,在android.widget包中。

 

单选按钮要声明在RadioGroup中,RadioGroup是RadioButton的承载体,程序运行时不可见,应用程序中可能包含一个或多个RadioGroup,RadioGroup是线性布局LinearLayout的子类。

单选按钮状态更改的监听,是要给它的RadioGroup添加setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener)监听器。注意监听器类型和复选按钮CheckBox是不相同的。

单选按钮的通常用法:

1、用xml描述的RadioGroup和RadioButton应用的界面设计
<?xml version="1.0" encoding="utf-8"?>
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="java" />
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dotNet" />
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PHP" />


2、引用处理程序

public void onCreate(Bundle savedInstanceState) {
       ......
        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup); 
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton) findViewById(checkedId);
                Log.i(TAG, String.valueOf(radioButton.getText()));
            }
        });
}
 RadioButton 和RadioGroup常用的方法及说明:

如下代码:
RadioGroup.check(R.id.dotNet);//将id名为dotNet的单选框设置成选中状态。
(RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());//获取被选中的单选框。
RadioButton.getText( );//获取单选框的值

(二)复选按钮CheckBox

复选按钮CheckBox,是一个同时可以选择多个选项的控件,继承自android.widget.CompoundButton,在android.widget包中。


复选按钮CheckBox的通常用法:
     1、用xml描述的CheckBox应用界面设计
<?xml version="1.0" encoding="utf-8"?>
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent">
      android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="java" />

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="dotNet" />
      android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="PHP" />