只有标题和文本内容简单对话框
代码与范例:
AlertDialog.Builder dlg;
dlg = new AlertDialog.Builder(AlertDialogSample.this);
dlg.setTitle("简单对话框");
dlg.setMessage("Hello, AlertDialog!");
dlg.show();
说明:生成一个只有标题与内容的简单对话框。
含有一个按钮的对话框
代码与范例:
AlertDialog.Builder dlg;
dlg = new AlertDialog.Builder(AlertDialogSample.this);
dlg.setTitle("但按钮对话框");
dlg.setMessage("Hello, AlertDialog!");
dlg.setPositiveButton("OK", null)
dlg.show();
说明:生成一个含有一个按钮的简单对话框,点击该按钮将关闭对话框。
含有两个按钮的对话框
代码与范例:
new AlertDialog.Builder(AlertDialogSample.this)
.setTitle("提示")
.setMessage("确定退出?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlertDialogSample.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.show();
说明:生成一个含有两个按钮的简单对话框。
含有三个按钮的对话框
代码与范例:
new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("YES/NO Dialog")
.setMessage("Is this a pen ?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* 这里是YES的处理 */
}
})
.setNeutralButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* 这里是NO的处理 */
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* 这里是Cancel的处理 */
}
})
.show();
说明:生成一个含有三个按钮的对话框。
含有一个单选列表的对话框
代码与范例:
final CharSequence[] items = {"红", "绿", "蓝"};
new AlertDialog.Builder(AlertDialogSample.this)
.setTitle("选择颜色")
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
button.setText(String.format("选择了%s。",items[item]));
}
})
.show();
说明:生成一个含有一个单选列表的对话框。
含有一个多选列表的对话框
代码与范例:
final CharSequence[] items = {"红", "绿", "蓝"};
final boolean[] checkedItems = {true, false, true};
new AlertDialog.Builder(AlertDialogSample.this)
.setTitle("Pick a color")
.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which,boolean isChecked) {
checkedItems[which]=isChecked;
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
for(int i=0;i
说明:生成一个含有一个多选列表的对话框。
含有Radio Button的对话框
代码与范例:
String[] str_items = {"红","绿","蓝"};
String result_item = "";
int def_index = 2;
result_item = str_items[def_index];
new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("选择颜色")
.setSingleChoiceItems(str_items, def_index,
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
result_item = str_items[which];
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
new AlertDialog.Builder(AlertDialogText.this)
.setTitle("color=" + result_item)
.show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
new AlertDialog.Builder(AlertDialogText.this)
.setTitle("Canceled")
.show();
}
})
.show();
}
说明:生成一个含有Radio Button的对话框。
自定义对话框
代码与范例:
LayoutInflater inflater = LayoutInflater.from(AlertDialogSample.this);
View layout = inflater.inflate(R.layout.custom_dialog,(ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("这是一个自定义对话框!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
new AlertDialog.Builder(AlertDialogSample.this)
.setView(layout)
.show();
其中代码的第一行也可以用以下两种之一替换来展开布局:
LayoutInflater inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = SampleDialog1.this.getLayoutInflater();
直接通过代码生成布局也可以,虽然不是太推荐这种做法:
LinearLayout layout = new LinearLayout(AlertDialogSample.this);
ImageView image = new ImageView(AlertDialogSample.this);
image.setImageResource(R.drawable.icon);
layout.addView(image);
TextView text = new TextView(AlertDialogSample.this);
text.setText("这是一个自定义对话框!");
layout.addView(text);
new AlertDialog.Builder(AlertDialogSample.this)
.setView(layout)
.show();
说明:生成一个含有自定义视图的对话框,其中用到了自定义的xml布局文件custom_dialog.xml。
综合对话框
代码与范例:
final CharSequence[] items = {"红", "绿", "蓝"};
final boolean[] checkedItems = {true, false, true};
TextView tv=new TextView(AlertDialogSample.this);
tv.setText("setView");
new AlertDialog.Builder(AlertDialogSample.this)
.setIcon(R.drawable.icon)
.setTitle("综合对话框")
.setView(tv)
.setMessage("Hello, World!")
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) { }
})
.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) { }
})
.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which,
boolean isChecked) { }
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlertDialogSample.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.show();
说明:综合使用上述几种对话框。
含有文本框的对话框
代码与范例:
et = new EditText(this);
new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("吃什么?")
.setView(et)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
new AlertDialog.Builder(AlertDialogText.this)
.setTitle("吃: " + edtInput.getText().toString())
.show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.show();
说明:生成一个能够输入文本的对话框。
含有进度条的对话框
代码与范例:
public class AlertDialogText extends Activity {
public ProgressDialog progDlg;
public Handler progHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 生成对话框
progDlg = new ProgressDialog(this);
progDlg.setIcon(R.drawable.icon);
progDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progDlg.setMax(100);
progDlg.setButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton) {
progDlg.cancel();
}
});
// 使进度条前进
progHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
int v = progDlg.getProgress();
if (v >= 100) {
progDlg.dismiss();
} else {
progDlg.incrementProgressBy(1);
progHandler.sendEmptyMessageDelayed(0, 100);
}
}
};
// 按下Ok按钮的反应
new AlertDialog.Builder(this)
.setTitle("PUSH START BUTTON")
.setPositiveButton("START", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
progDlg.show();
progHandler.sendEmptyMessage(0);
}
})
.show();
}
}
说明:生成一个含有进度条的对话框。
含有正在运行标识的对话框
代码与范例:
ProgressDialog dialog = ProgressDialog.show(AlertDialogSample.this, "","载入中,请稍候", true);
说明:生成一个含有一个正在运行的循环旋转标识的简单对话框。