1.显示内容
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("对话框标题");
builder.setMessage("对话框内容");
AlertDialog dialog = builder.create();
dialog.show();
//new AlertDialog.Builder(MainActivity.this).setTitle("标题").setMessage("内容").create().show();大神写法
2.赞成/中立/反对按钮
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("标题");
builder.setMessage("内容");
builder.setNegativeButton("反对", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "反对派", Toast.LENGTH_LONG).show();
}
});
builder.setNeutralButton("中立", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "中立派", Toast.LENGTH_LONG).show();
}
});
builder.setPositiveButton("赞同", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "赞同派",Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
3.单选框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("旅游");
final String[] items = new String[]{"国外游","省外游","郊游"};
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("旅游");
final String[] items = new String[]{"国外游","省外游","郊游"};
builder.setSingleChoiceItems(items, checkedItem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
checkedItem = which;
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, items[checkedItem], Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
4.复选框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("你喜欢哪部四大名著?");
final String[] items = new String[]{"西游记","三国演义","水浒传","红楼梦"};
final boolean[] checkedItems = new boolean[]{true,false,false,false};
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which] = isChecked;
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String str = "";
for(int i = 0;i<checkedItems.length;i++){
if(checkedItems[i]){
str +=items[i]+ " ";
}
}
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
5.自定义
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("标题");
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View view = inflater.inflate(R.layout.dialog, null);
builder.setView(view);
AlertDialog dialog = builder.create();
dialog.show();
注:
获得LayoutInflater对象有三种方法(效果一样):
*通过LayoutInflater的静态方法from()获得
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
*通过Activity的getLayoutInflater()方法获得
LayoutInflater inflater = this.getLayoutInflater();
*通过系统服务获取
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
然后
View view = inflater.inflate(R.layout.activity_main, null);
setContentView(view);
6.进度条
final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("标题");
dialog.setMessage("内容");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress = 0;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
progress+=10;
if(progress>100){
progress=100;
dialog.dismiss();
}
dialog.setProgress(progress);
}
}, 1000 , 1000);
dialog.show();
7.显示Item的对话框
private String[] items = {"魏","蜀","吴"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,items[which],Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
效果图:
8.显示listview的对话框
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,new String[]{"张三","李四"});
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
效果图:
9.显示自定义view的对话框
View view = LayoutInflater.from(this).inflate(R.layout.activity_main,null);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(view);
AlertDialog dialog = builder.create();
dialog.show();
效果图: