chechbox 的颜色android,Android 之 CheckBox 详解

CheckBox,也就是多项选择。Android中提供了ChechBox控件,使用起来非常方便。这里还是老规矩,用一个Demo来演示

1:新建一个工程:CheckBoxDemo

2:布局文件如下:

3:源代码如下:这种是常见的一种表达形式。

package com.rocky.studio.ch426;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.TextView;

import android.widget.Toast;

public class CheckBoxDemo extends Activity {

private TextView m_txtView;

private CheckBox m_CheckBox1;

private CheckBox m_CheckBox2;

private CheckBox m_CheckBox3;

private CheckBox m_CheckBox4;

private Button m_Button;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

m_txtView=(TextView)this.findViewById(R.id.TextView01);

m_CheckBox1=(CheckBox)this.findViewById(R.id.CheckBox01);

m_CheckBox2=(CheckBox)this.findViewById(R.id.CheckBox02);

m_CheckBox3=(CheckBox)this.findViewById(R.id.CheckBox03);

m_CheckBox4=(CheckBox)this.findViewById(R.id.CheckBox04);

m_txtView.setText("调查:你喜欢Android是因为什么原因?");

m_CheckBox1.setText("好看");

m_CheckBox2.setText("好用");

m_CheckBox3.setText("免费");

m_CheckBox4.setText("应用广泛");

m_CheckBox1.setOnCheckedChangeListener(m_checkboxListener);

m_CheckBox2.setOnCheckedChangeListener(m_checkboxListener);

m_CheckBox3.setOnCheckedChangeListener(m_checkboxListener);

m_CheckBox4.setOnCheckedChangeListener(m_checkboxListener);

m_Button=(Button)this.findViewById(R.id.Button01);

m_Button.setOnClickListener(m_BtnListener);

m_Button.setText("提交");

}

private OnClickListener m_BtnListener=new OnClickListener()

{

public void onClick(View arg0) {

// TODO Auto-generated method stub

if(arg0.getId()==R.id.Button01)

{

int value=0;

if(m_CheckBox1.isChecked())

{

value++;

}

if(m_CheckBox2.isChecked())

{

value++;

}

if(m_CheckBox3.isChecked())

{

value++;

}

if(m_CheckBox4.isChecked())

{

value++;

}

Toast.makeText(getBaseContext(), "你选择了 "+value+"项", Toast.LENGTH_SHORT).show();

}

}

};

private CheckBox.OnCheckedChangeListener m_checkboxListener =new CheckBox.OnCheckedChangeListener()

{

public void onCheckedChanged(CompoundButton buttonView,

boolean isChecked) {

// TODO Auto-generated method stub

if(buttonView.getId()==R.id.CheckBox01)

{

if(isChecked)

{

Toast.makeText(getBaseContext(), "CheckBox 01 check ", Toast.LENGTH_SHORT).show();

}

else

{

Toast.makeText(getBaseContext(), "CheckBox 01 ucheck ", Toast.LENGTH_SHORT).show();

}

}

if(buttonView.getId()==R.id.CheckBox02)

{

if(isChecked)

{

Toast.makeText(getBaseContext(), "CheckBox 02 check ", Toast.LENGTH_SHORT).show();

}

else

{

Toast.makeText(getBaseContext(), "CheckBox 02 ucheck ", Toast.LENGTH_SHORT).show();

}

}

if(buttonView.getId()==R.id.CheckBox03)

{

if(isChecked)

{

Toast.makeText(getBaseContext(), "CheckBox 03 check ", Toast.LENGTH_SHORT).show();

}

else

{

Toast.makeText(getBaseContext(), "CheckBox 03 ucheck ", Toast.LENGTH_SHORT).show();

}

}

if(buttonView.getId()==R.id.CheckBox04)

{

if(isChecked)

{

Toast.makeText(getBaseContext(), "CheckBox 04 check ", Toast.LENGTH_SHORT).show();

}

else

{

Toast.makeText(getBaseContext(), "CheckBox 04 ucheck ", Toast.LENGTH_SHORT).show();

}

}

}

};

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值