<1>简介
今天学习AlertDialog,以及单选列表dialog和复选列表dialog。
<2>关键步骤
新建一个字符串数组资源文件/res/values/array.xml,用于提供单选列表dialog和复选列表dialog的数据。
还是通过showDialog函数来调用创建Dialog,通过AlertDialog.Builder来创建相应的AlertDialog
<3>出现的问题
无
<4>代码及解释
AlertDialog效果图:
单选列表dialog效果图:
复选列表dialog效果图:
array.xml:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<string-array name="msa">
<item>游泳</item>
<item>打篮球</item>
<item>写Java程序</item>
</string-array>
<string-array name="city">
<item>北京</item>
<item>上海</item>
<item>深圳</item>
</string-array>
</resources>
AlertDialogActivity:
package com.ui.dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.test.R;
public class AlertDialogActivity extends Activity{
/**
* @ProjectName: [androidtest]
* @Package: [com.ui.dialog.AlertDialogActivity.java]
* @ClassName: [AlertDialogActivity]
* @Description:
* @Author: [gmj]
* @CreateDate: [2013-8-29 下午7:47:55]
* @Version: [v1.0]
*/
boolean[] mulFlags = new boolean[]{false , false , false};
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
Button b = (Button)this.findViewById(R.id.buttondialog);
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new AlertDialog.Builder(AlertDialogActivity.this)
.setTitle("消息提示").setMessage("this is a alertdialog")
.setPositiveButton(
"sure",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
).show();
}
});
Button bt = (Button)this.findViewById(R.id.buttondialog2);
bt.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog(0);
}
});
Button bt2 = (Button)this.findViewById(R.id.buttondialog3);
bt2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog(1);
}
});
}
public Dialog onCreateDialog(int id)
{
Dialog dialog = null;
switch(id)
{
case 0:
Builder bu = new AlertDialog.Builder(this);
bu.setIcon(R.drawable.ic_launcher);
bu.setTitle("单选列表对话框");
bu.setSingleChoiceItems(R.array.msa,
0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
EditText et = (EditText)findViewById(R.id.editdialog);
et.setText("you choose:" + getResources().getStringArray(R.array.msa)[which]);
}
}
);
bu.setPositiveButton(
"sure",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog = bu.create();
break;
case 1:
Builder bu2 = new AlertDialog.Builder(this);
bu2.setIcon(R.drawable.ic_launcher);
bu2.setTitle("复选列表对话框");
bu2.setMultiChoiceItems(R.array.city, mulFlags,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
mulFlags[which] = isChecked;
String resultMsg = "you choose:";
for(int i = 0 ; i < mulFlags.length ; i++)
{
if(mulFlags[i])
resultMsg = resultMsg + getResources().getStringArray(R.array.city)[i] + "、";
}
EditText et = (EditText)findViewById(R.id.editdialog);
et.setText(resultMsg);
}
});
bu2.setPositiveButton(
"sure",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog = bu2.create();
break;
}
return dialog;
}
}