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