CheckBox,也就是多项选择。Android中提供了ChechBox控件,使用起来非常方便。
CheckBox效果
main.xml源码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/text"/>
<CheckBox
android:id="@+id/check1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="苹果 ios" />
<CheckBox
android:id="@+id/check2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="谷歌 Android" />
<CheckBox
android:id="@+id/check3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RIM BlackBerry" />
<CheckBox
android:id="@+id/check4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="微软 Windows phone 7" />
<CheckBox
android:id="@+id/check5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="诺基亚 symbian" />
<Button
android:id="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="确定" />
</LinearLayout>
CheckBox 事件响应setOnCheckedChangeListener
本程序java源码
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class CheckBoxActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final CheckBox check1 = (CheckBox)findViewById(R.id.check1);
final CheckBox check2 = (CheckBox)findViewById(R.id.check2);
final CheckBox check3 = (CheckBox)findViewById(R.id.check3);
final CheckBox check4 = (CheckBox)findViewById(R.id.check4);
final CheckBox check5 = (CheckBox)findViewById(R.id.check5);
//创建CheckBox事件监听器
check1.setOnCheckedChangeListener(listener);
check2.setOnCheckedChangeListener(listener);
check3.setOnCheckedChangeListener(listener);
check4.setOnCheckedChangeListener(listener);
check5.setOnCheckedChangeListener(listener);
}
private OnCheckedChangeListener listener = new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
switch(buttonView.getId())
{
case R.id.check1:
if(isChecked)
Toast.makeText(getApplicationContext(), "你喜欢苹果 ios智能手机系统", Toast.LENGTH_LONG).show();
break;
case R.id.check2:
if(isChecked)
Toast.makeText(getApplicationContext(), "你喜欢谷歌 Android智能手机系统", Toast.LENGTH_LONG).show();
break;
case R.id.check3:
if(isChecked)
Toast.makeText(getApplicationContext(), "你喜欢RIM BlackBerry智能手机系统",Toast.LENGTH_LONG).show();
break;
case R.id.check4:
if(isChecked)
Toast.makeText(getApplicationContext(), "你喜欢微软 Windows phone 7智能手机系统", Toast.LENGTH_LONG).show();
break;
case R.id.check5:
if(isChecked)
Toast.makeText(getApplicationContext(), "你喜欢诺基亚 symbian智能手机系统", Toast.LENGTH_LONG).show();
break;
}
}
};