Android Dialog的四种情况 (转)
四种dialog总结,下面的代码返回的是一个Dialog,在onClick里面用showDialog(anyDialog)调用就可以。
一、一个确认一个取消
private Dialog buildDialog1(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.alert_dialog_icon);
builder.setTitle(R.string.alert_dialog_two_buttons_title);
builder.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的确定按钮");
}
});
builder.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的取消按钮");
}
});
return builder.create();
}
二、带有大量文本
private Dialog buildDialog2(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.alert_dialog_icon);
builder.setTitle(R.string.alert_dialog_two_buttons_msg);
builder.setMessage(R.string.alert_dialog_two_buttons2_msg);
builder.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的确定按钮");
}
});
builder.setNeutralButton(R.string.alert_dialog_something,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的进入详细按钮");
}
});
builder.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的取消按钮");
}
});
return builder.create();
}
三、带输入框的,稍微复杂一点,要加人带textview、editview的layout
private Dialog buildDialog3(Context context) {
LayoutInflater inflater = LayoutInflater.from(this);
final View textEntryView = inflater.inflate(
R.layout.alert_dialog_text_entry, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.alert_dialog_icon);
builder.setTitle(R.string.alert_dialog_text_entry);
builder.setView(textEntryView);
builder.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的确定按钮");
}
});
builder.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的取消按钮");
}
});
return builder.create();
}
四、带进度条的
private Dialog buildDialog4(Context context) {
ProgressDialog dialog = new ProgressDialog(context);
dialog.setTitle("正在下载歌曲");
dialog.setMessage("请稍候……");
return dialog;
}