Button simple = null;
Button list = null;
Button single = null;
Button progress = null;
Button custom = null;
private final static int SIMPLEALERTDIALOG = 1;
private final static int LISTALERTDIALOG = 2;
private final static int SINGLEALERTDIALOG = 3;
private final static int PROGRESSALERTDIALOG = 4;
private final static int CUSTOMALERTDIALOG = 5;
private static final int MAX = 100;
String[] items;
private AlertDialog alert;
private ProgressDialog progressdialog;
switch (id)
{
case SIMPLEALERTDIALOG://是否退出本软件
Log.i("ss", "onCreateDialog");
builder.setMessage(" 您要退出本软件么?");
builder.setCancelable(false);
builder.setPositiveButton("是", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
MainActivity.this.finish();
}
});
builder.setNegativeButton("否", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
return builder.create();
case LISTALERTDIALOG://一个ListView的dialog,选择一项 dialog自动消失
builder.setTitle("请选择一种颜色:");
builder.setItems(items, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
}
});
return builder.create();
case SINGLEALERTDIALOG://单选RadioButton的dialog
builder.setTitle("请选择一种颜色:");
builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
if (null != alert)
{
alert.dismiss();
}
}
});
alert = builder.create();
return alert;
case PROGRESSALERTDIALOG:
//复选框CheckButton的dialog
// builder.setTitle("请选择一种颜色:");
// builder.setMultiChoiceItems(items, null, new
// DialogInterface.OnMultiChoiceClickListener()
// {
//
// @Override
// public void onClick(DialogInterface dialog, int which, boolean
// isChecked)
// {
// if (isChecked)
// {
// Toast.makeText(MainActivity.this, items[which],
// Toast.LENGTH_SHORT).show();
// }
// }
// });
//进度条dialog
progressdialog = new ProgressDialog(this);
progressdialog.setTitle("进度条!");
progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressdialog.setMax(MAX);
// progressdialog.setProgress(50);
progressdialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, "" + progs, Toast.LENGTH_SHORT).show();
}
});
progressdialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
progressdialog.dismiss();
}
});
return progressdialog;
case CUSTOMALERTDIALOG://自定义dialog,外加取得dialog中的EditText
LayoutInflater inflater = LayoutInflater.from(this);
final View view = inflater.inflate(R.layout.dialogs, null);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("自定义对话框");
builder.setView(view);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
et1 = (EditText) (view.findViewById(R.id.Name));
et2 = (EditText) (view.findViewById(R.id.Password));