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;
}
}