Android控件之CheckBox、RadioButton

1、复选框CheckBox控件

public class CheckBox extend CompoundButton

复选框允许用户选择一个或多个选项。通常情况下,是从一个垂直列表中选择多个选项。每个复选框是分开管理的,必须为每个复选框注册监听事件。注册click监听事件的方式有两种。

xml布局文件如下:

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="23dp"
        android:text="看小说" />
    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox1"
        android:layout_below="@+id/checkBox1"
        android:text="打网游" />
    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox2"
        android:layout_below="@+id/checkBox2"
        android:text="看电影" />

第一种:在使用该xml布局文件的Activity类实现CompoundButton.OnCheckedChangeListener接口

package com.example.checkboxradiobutton;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener {

	private CheckBox cb1,cb2,cb3;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		cb1 = (CheckBox) this.findViewById(R.id.checkBox1);
		cb2 = (CheckBox) this.findViewById(R.id.checkBox2);
		cb3 = (CheckBox) this.findViewById(R.id.checkBox3);
		
		cb1.setOnCheckedChangeListener(this);
		cb2.setOnCheckedChangeListener(this);
		cb3.setOnCheckedChangeListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * @param compoundButton 被单击的复选框
	 * @param isChecked 复选框选中为true,没有选中为false
	 */
	@Override
	public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
		switch (compoundButton.getId()) {
		case R.id.checkBox1:
			if (isChecked) Toast.makeText(this, "选中看小说", Toast.LENGTH_SHORT).show();
			else Toast.makeText(this, "取消看小说", Toast.LENGTH_SHORT).show();
			break;
		case R.id.checkBox2:
			if (isChecked) Toast.makeText(this, "选中打网游", Toast.LENGTH_SHORT).show();
			else Toast.makeText(this, "取消打网游", Toast.LENGTH_SHORT).show();
			break;
		case R.id.checkBox3:
			if (isChecked) Toast.makeText(this, "选中看电影", Toast.LENGTH_SHORT).show();
			else Toast.makeText(this, "取消看电影", Toast.LENGTH_SHORT).show();
			break;
		}
		
	}

}

第二种:使用内部类的方式,对每一个复选框注入单击事件

cb1 = (CheckBox) this.findViewById(R.id.checkBox1);
		cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
				
			}
		});

2、单选框RadioButton控件

单选按钮允许用户从一组中选择一个选项。由于单选按钮是相互排斥的,通过RadioGroup将多个RadioButton组合到一起,这样可以确保只能选择一个选项。

<RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox3"
        android:layout_below="@+id/textView2" >
        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="92dp"
            android:layout_height="match_parent"
            android:checked="true"
            android:text="男" />
        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" />
</RadioGroup>

事件处理:onRadioButtonClicked。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值