建立3种常用对话框

一、普通对话框
private void showDialog() {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("消息通知");
builder.setMessage("确定退出?" );
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

        }
    });
    Dialog dialog=builder.create();
    dialog.show();
}

二、可输入对话框
先建立一个布局文件,把所需要的添加上;
例:我建立了一个login的布局文件
private void showDialog() {
final ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("标题");

    progress.setMessage("加载中...");
    progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progress.setMax(100);//最大进度

progress.show();
//更新progress的显示进度用到线程
new Thread(new Runnable() {
@Override
public void run() {
int i=0;
while (i<100){
try{
Thread.sleep(200);
progress.incrementProgressBy(5);//进度更新一次走5%
i+=5;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progress.dismiss();
}
}).start();
}

你可能感兴趣的:(建立3种常用对话框)