AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("通知对话框");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("是否升级程序?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@OverridepublicvoidonClick(DialogInterface dialogInterface, int i) {
Log.i("Click","PositiveButton");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@OverridepublicvoidonClick(DialogInterface dialogInterface, int i) {
Log.i("Click","NegativeButton");
}
});
//builder.create();
builder.show();
单选对话框(Dialog SingleChoice)
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("单选对话框");
final String[] items = new String[]{"Mode_DZ","Mode_DD","Mode_DEMO"};
builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
@OverridepublicvoidonClick(DialogInterface dialogInterface, int i) {
Log.i("Click2",items[i]+" 被点击了");
dialogInterface.cancel();
}
});
builder.show();
多选对话框(Dialog MultiChoice)
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("多选对话框");
final String[] items = new String[]{"Mode_DZ","Mode_DD","Mode_DEMO"};
boolean[] status = newboolean[]{true,true,false};
builder.setMultiChoiceItems(items, status, new DialogInterface.OnMultiChoiceClickListener() {
@OverridepublicvoidonClick(DialogInterface dialogInterface, int i, boolean b) {
Log.i("Click3",items[i]+"被点击了"+b);
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@OverridepublicvoidonClick(DialogInterface dialogInterface, int i) {
}
});
builder.show();
进度条对话框(ProgressDialog)
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("提醒");
dialog.setMessage("正在加载中...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.show();
new Thread(new Runnable() {
@Overridepublicvoidrun() {
for (int i = 0; i < 100; i++) {
dialog.setProgress(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
dialog.dismiss();
}
}).start();
按xml布局的对话框
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.posrangedlg, null);
etMinPos = (EditText) textEntryView.findViewById(R.id.et_MinPosition);
etMaxPos = (EditText) textEntryView.findViewById(R.id.et_MaxPosition);
AlertDialog dlg = new AlertDialog.Builder(this)
.setTitle("输入位置范围")
.setView(textEntryView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String min = etMinPos.getText().toString();
String max = etMaxPos.getText().toString();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.create();
dlg.show();