Android之复选按钮、单选按钮、开关按钮

时间:2025-03-14 08:35:48

Android的复选按钮、单选按钮、开关按钮是经常使用的几个组件。下面给出一个例子

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RadioGroup                        //单选按钮组
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton                     //单选按钮
            android:
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"        //默认选中
            android:text="man" />

        <RadioButton
            android:
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="woman" />

        <RadioButton
            android:
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="no" />
    </RadioGroup>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <CheckBox                         //复选框
            android:
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="android" />

        <CheckBox
            android:
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="java" />

        <CheckBox
            android:
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="net" />
    </LinearLayout>

    <ToggleButton                          //开关按钮
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="关灯"
        android:textOn="开灯" />

</LinearLayout>

 

对应的Java代码。要注意的是他们要实现的监听接口不同。

 

package ;

import ;
import ;
import .*;
import ;

public class CheckActivity extends Activity implements
		 {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		(savedInstanceState);
		setContentView(.check_main);

		RadioGroup rg = (RadioGroup) findViewById(); // 得到单选按钮组
		(new OnCheckedChangeListener() { // 设置选择监听

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {// 根据checkedId获得选择的按钮
				RadioButton rb = (RadioButton) findViewById(checkedId);
				(, (),
						Toast.LENGTH_SHORT).show();
			}
		});
		// 得到复选按钮框
		CheckBox cba = (CheckBox) findViewById();
		CheckBox cbj = (CheckBox) findViewById();
		CheckBox cbn = (CheckBox) findViewById();
		(this);// 设置选择监听
		(this);
		(this);

		final ToggleButton tb = (ToggleButton) findViewById();
		(new () {
			// 注意,此处实现的是CompoundButton的接口
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
			}
		});
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			(, (),
					Toast.LENGTH_SHORT).show();
		}
	}
}