checkbox android:button,android RadioButton和CheckBox组件的使用方法

package com.example.control1;

import android.app.Activity;

import android.os.Bundle;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity {

//定义各控件的变量

private TextView who = null;

private TextView how = null;

private RadioGroup who_group = null;

private RadioButton china = null;

private RadioButton america = null;

private RadioButton others = null;

private CheckBox less = null;

private CheckBox thirty = null;

private CheckBox forty = null;

private CheckBox fifty = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//获得对应的控件

who = (TextView)findViewById(R.id.who);

how = (TextView)findViewById(R.id.how);

who_group = (RadioGroup)findViewById(R.id.who_group);

china = (RadioButton)findViewById(R.id.china);

america = (RadioButton)findViewById(R.id.america);

others = (RadioButton)findViewById(R.id.others);

less = (CheckBox)findViewById(R.id.less);

thirty = (CheckBox)findViewById(R.id.thirty);

forty = (CheckBox)findViewById(R.id.forty);

fifty = (CheckBox)findViewById(R.id.fifty);

//设置who_group的监听器,其实是一句代码,其参数是一个带有重构函数的对象

who_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

public void onCheckedChanged(RadioGroup group, int checkedId) {

// TODO Auto-generated method stub

if(checkedId == china.getId()){

Toast.makeText(MainActivity.this,"中国", Toast.LENGTH_SHORT).show();

}

else if(checkedId == america.getId()){

Toast.makeText(MainActivity.this, "美国", Toast.LENGTH_SHORT).show();

}

else if(checkedId == others.getId()){

Toast.makeText(MainActivity.this, "其它国家", Toast.LENGTH_SHORT).show();

}

}

});

//下面为4个checkbox多选按钮分别建立监听器

less.setOnCheckedChangeListener(new OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

// TODO Auto-generated method stub

if(isChecked)

{

Toast.makeText(MainActivity.this, "30个以下", Toast.LENGTH_SHORT).show();

}

else{

Toast.makeText(MainActivity.this, "不是30个以下", Toast.LENGTH_SHORT).show();

}

}

});

//下面为4个checkbox多选按钮分别建立监听器

thirty.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

// TODO Auto-generated method stub

if(isChecked)

{

Toast.makeText(MainActivity.this, "30~39", Toast.LENGTH_SHORT).show();

}

else{

Toast.makeText(MainActivity.this, "不是30~39", Toast.LENGTH_SHORT).show();

}

}

});

//下面为4个checkbox多选按钮分别建立监听器

forty.setOnCheckedChangeListener(new OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

// TODO Auto-generated method stub

if(isChecked)

{

Toast.makeText(MainActivity.this, "40~49", Toast.LENGTH_SHORT).show();

}

else{

Toast.makeText(MainActivity.this, "不是40~49", Toast.LENGTH_SHORT).show();

}

}

});

//下面为4个checkbox多选按钮分别建立监听器

fifty.setOnCheckedChangeListener(new OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

// TODO Auto-generated method stub

if(isChecked)

{

Toast.makeText(MainActivity.this, "50以上", Toast.LENGTH_SHORT).show();

}

else{

Toast.makeText(MainActivity.this, "不是50以上", Toast.LENGTH_SHORT).show();

}

}

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值