android开发控件——checkbox(入门)

checkbox是多选按钮,其本身是一种特殊的button,允许多选或者单选。选择可以激发OnCheckedChangeListener事件,接下来来演示。

先来看效果


先来看布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 <CheckBox 
     android:id="@+id/u_cb1"
    android:button="@android:drawable/btn_star"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="cb1"
     android:paddingLeft="200dip"
     android:background="#87CEFA"
     />  
  <CheckBox
      android:id="@+id/u_cb2"
     android:button="@android:drawable/btn_star"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="cb2"
     android:paddingLeft="200dip"
     android:layout_marginTop="20dip"
      android:background="#87CEFA"
      />
    <CheckBox 
        android:id="@+id/u_cb3"
       android:button="@android:drawable/btn_star"
        android:text="cb3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="200dip"
        android:layout_marginTop="20dip"
         android:background="#87CEFA"
        />
 <CheckBox 
     android:id="@+id/u_cb4"
    android:button="@android:drawable/btn_star"
     android:text="cb4"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:paddingLeft="200dip"
     android:layout_marginTop="20dip"
      android:background="#87CEFA"
     
     />
 <TextView 
     android:id="@+id/u_tv1"
     android:textColor="#0000FF"
     android:textSize="20dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_marginTop="20dip"
     android:gravity="center"

     
     />
</LinearLayout>

请注意checkbox主要运用的属性是:

1. android:button,这里可以设置按钮的样式,@android:drawable/btn_star就是图中的星型按钮,当然还有很多都在@android:drawable/之下。

2.  android:paddingLeft="200dip",是在设置text出现在checkbox左边200dip的位置

下面来看代码:

package com.example.testcheckbox;

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

public class MainActivity extends Activity {
private CheckBox cb1=null;
private CheckBox cb2=null;
private CheckBox cb3=null;
private CheckBox cb4=null;
private TextView tv1=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user);
cb1=(CheckBox)findViewById(R.id.u_cb1);
cb2=(CheckBox)findViewById(R.id.u_cb2);
cb3=(CheckBox)findViewById(R.id.u_cb3);
cb4=(CheckBox)findViewById(R.id.u_cb4);
tv1=(TextView)findViewById(R.id.u_tv1);
cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {//此处设置了当多选框被选中以后激发的事件代码

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {//第二个参数的意义是某个button选中则为True
if(isChecked)//如果cb1被选中
{
Toast.makeText(getApplicationContext(), cb1.getText()+"被选中", Toast.LENGTH_LONG).show();
tv1.setText(cb1.getText()+"被选中"+"  ");
}

}
});
cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {//同上

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
Toast.makeText(getApplicationContext(), cb2.getText()+"被选中", Toast.LENGTH_LONG).show();
tv1.setText(cb2.getText()+"被选中"+"  ");
}

}
});
cb3.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(getApplicationContext(), cb3.getText()+"被选中", Toast.LENGTH_LONG).show();
tv1.setText(cb3.getText()+"被选中"+"  ");
}
}
});
cb4.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(getApplicationContext(), cb4.getText()+"被选中", Toast.LENGTH_LONG).show();
tv1.setText(cb4.getText()+"被选中"+"  ");
}
}
});


}

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

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值