代码:
package com.test.dialog;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class DialogTestActivity extends Activity {
/** Called when the activity is first created. */
public static final String TAG="DialogTestActivity";
int i=0;
boolean b[]={true,false,false,false};
Button btnAlertDialog;
Button btnSingleItems;
Button btnSingelChoices;
Button btnMuiltiChoice;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnAlertDialog = (Button)findViewById(R.id.alertDialog);
btnAlertDialog.setOnClickListener(new ButtonViewClickListener());
btnSingleItems = (Button)findViewById(R.id.singleItemsAlertDialog);
btnSingleItems.setOnClickListener(new ButtonViewClickListener());
btnSingelChoices = (Button)findViewById(R.id.singleChoiceAlertDialog);
btnSingelChoices.setOnClickListener(new ButtonViewClickListener());
btnMuiltiChoice = (Button)findViewById(R.id.multiItemsAlertDialog);
btnMuiltiChoice.setOnClickListener(new ButtonViewClickListener());
//单选框的测试
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
RadioButton radioButton = (RadioButton)findViewById(checkedId);
Log.d(TAG, String.valueOf(radioButton.getText().toString())+","+checkedId);
}
});
//多选框的测试
final ArrayList<CheckBox> list = new ArrayList<CheckBox>();
list.add((CheckBox)findViewById(R.id.cbAndroid));
list.add((CheckBox)findViewById(R.id.cbJava));
list.add((CheckBox)findViewById(R.id.cbPhp));
list.add((CheckBox)findViewById(R.id.cbWP7));
for(CheckBox box : list){
box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
CheckBox box = (CheckBox) buttonView;
Log.d(TAG, isChecked+","+box.getText().toString());
}
});
}
Button button = (Button)findViewById(R.id.btnCheckBox);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(CheckBox box : list){
if(box.isChecked()){
Log.d(TAG, box.getText().toString());
}
}
}
});
}
public class ButtonViewClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.alertDialog:
myAlertDialog();
break;
case R.id.singleItemsAlertDialog:
mySingleItemsDialog();
break;
case R.id.singleChoiceAlertDialog:
mySingleChoiceDialog();
break;
case R.id.multiItemsAlertDialog:
myMultiItemsDialog();
break;
}
}
}
/**
* 对话通知框
*/
public void myAlertDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("AlertDialog");
builder.setMessage("我是内容!");
builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.d(TAG, ""+which);
dialog.cancel();
}});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.d(TAG, ""+which);
dialog.cancel();
}
});
builder.create();
builder.show();
}
/**
* 单选列表对话框
*/
public void mySingleItemsDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("课程列表");
builder.setItems(R.array.singleItems, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.d(TAG, ""+which);
dialog.cancel();
}
});
builder.create().show();
}
/**
* 单选选项对话框
*/
public void mySingleChoiceDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("课程列表");
builder.setSingleChoiceItems(R.array.singleItems, i, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
i=which;
Log.d(TAG, ""+which);
dialog.cancel();
}
});
builder.create().show();
}
/**
* 多选项列表对话框
*/
public void myMultiItemsDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("课程列表");
builder.setMultiChoiceItems(R.array.singleItems, b, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
b[which] = isChecked;
Log.d(TAG, ""+which+","+isChecked);
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
builder.create().show();
}
}
xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="通知对话框AlertDialog"
android:id="@+id/alertDialog"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="单选列表AlertDialog"
android:id="@+id/singleItemsAlertDialog"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="单选选择AlertDialog"
android:id="@+id/singleChoiceAlertDialog"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="多选项列表对话框AlertDialog"
android:id="@+id/multiItemsAlertDialog"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/darker_gray"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="单选框RadioButton"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/radioGroup"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="java"
android:id="@+id/rbJava"
android:checked="true"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C#"
android:id="@+id/rbC"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="php"
android:id="@+id/rbPhp"
/>
</RadioGroup>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/darker_gray"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="多选框CheckBox"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="java"
android:id="@+id/cbJava"
android:checked="true"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android"
android:id="@+id/cbAndroid"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WP7"
android:id="@+id/cbWP7"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="php"
android:id="@+id/cbPhp"
/>
</LinearLayout>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="多选框CheckBox目前的值"
android:id="@+id/btnCheckBox"
/>
</LinearLayout>
调试输出的log