android中有许多地方会用到dialog提示框,这里我总结一下dialog的各种用法,写了一个工具类DialogManager.java实现所有的dialog显示。
- 等待提示框
/**
* 获取一个等待提示框
* @param context
* @param message
* @return
*/
public static ProgressDialog getProgressDialog(Context context, String message){
ProgressDialog dialog = new ProgressDialog(context);
dialog.setMessage(message);
return dialog;
}
2.信息提示框
/**
* 获取一个不带图标的提示框
*/
public static Dialog getMessageDialog(Context context, String title, String message,
String sureButton,
DialogInterface.OnClickListener listener ) {
builder = getBuilder(context);
builder.setTitle(title)
.setMessage(message)
.setNegativeButton("取消",null)
.setPositiveButton(sureButton,listener);
return builder.create();
}
3.单选提示框
public static Dialog getSingleItemDialog(Context context,String title,String[] items,int choiceItem,
DialogInterface.OnClickListener itemListener,
DialogInterface.OnClickListener listener){
builder = getBuilder(context);
builder.setTitle(title);
builder.setSingleChoiceItems(items,choiceItem,itemListener);
builder.setPositiveButton("确定", listener);
builder.setNegativeButton("取消", null);//null===关闭对话框
return builder.create();
}
4.多选提示框
public static Dialog getMultiItemDialog(Context context,String title,String[] items,boolean[] choiceItems,
DialogInterface.OnMultiChoiceClickListener multiListener,
DialogInterface.OnClickListener listener){
builder = getBuilder(context);
builder.setTitle(title)
.setMultiChoiceItems(items, choiceItems, multiListener)
.setPositiveButton("确定", listener);
return builder.create();
}
5.自定义底部dialog
这是我以前写得一个关于自定义底部提示框博客
下面贴出完整的代码:
package com.lxy.dialog.util;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
public class DialogManager {
protected static AlertDialog.Builder builder;
public static AlertDialog.Builder getBuilder(Context context){
builder = new AlertDialog.Builder(context);
return builder;
}
public DialogManager getMBuilder(Context context ){
builder = new AlertDialog.Builder(context);
return this;
}
/**
* 获取一个等待提示框
* @param context
* @param message
* @return
*/
public static ProgressDialog getProgressDialog(Context context, String message){
ProgressDialog dialog = new ProgressDialog(context);
dialog.setMessage(message);
return dialog;
}
/**
* 获取一个不带图标的提示框
*/
public static Dialog getMessageDialog(Context context, String title, String message,
String sureButton,
DialogInterface.OnClickListener listener ) {
builder = getBuilder(context);
builder.setTitle(title)
.setMessage(message)
.setNegativeButton("取消",null)
.setPositiveButton(sureButton,listener);
return builder.create();
}
/**
* 获取一个带图标的提示框
*/
public static Dialog getMessageDialog(Context context,int icon, String title, String message,
String sureButton,
DialogInterface.OnClickListener listener ) {
builder = getBuilder(context);
builder.setTitle(title)
.setIcon(icon)
.setMessage(message)
.setNegativeButton("取消",null)
.setPositiveButton(sureButton,listener);
return builder.create();
}
public static Dialog getMessageDialog(Context context, Drawable icon, String title, String message,
String sureButton,
DialogInterface.OnClickListener listener ) {
builder = getBuilder(context);
builder.setTitle(title)
.setIcon(icon)
.setMessage(message)
.setNegativeButton("取消",null)
.setPositiveButton(sureButton,listener);
return builder.create();
}
/**
* 获取一个不带图标单按钮的提示框
*/
public static Dialog getSingleMessageDialog(Context context, String title, String message,
String sureButton,
DialogInterface.OnClickListener listener ) {
builder = getBuilder(context);
builder.setTitle(title)
.setMessage(message)
.setPositiveButton(sureButton,listener);
return builder.create();
}
public static Dialog getSingleMessageDialog(Context context,int icon, String title, String message,
String sureButton,
DialogInterface.OnClickListener listener ) {
builder = getBuilder(context);
builder.setTitle(title)
.setIcon(icon)
.setMessage(message)
.setPositiveButton(sureButton,listener);
return builder.create();
}
public static Dialog getSingleMessageDialog(Context context,Drawable icon, String title, String message,
String sureButton,
DialogInterface.OnClickListener listener ) {
builder = getBuilder(context);
builder.setTitle(title)
.setIcon(icon)
.setMessage(message)
.setPositiveButton(sureButton,listener);
return builder.create();
}
public static Dialog getSingleItemDialog(Context context,String title,String[] items,int choiceItem,
DialogInterface.OnClickListener itemListener,
DialogInterface.OnClickListener listener){
builder = getBuilder(context);
builder.setTitle(title);
builder.setSingleChoiceItems(items,choiceItem,itemListener);
builder.setPositiveButton("确定", listener);
builder.setNegativeButton("取消", null);//null===关闭对话框
return builder.create();
}
public static Dialog getMultiItemDialog(Context context,String title,String[] items,boolean[] choiceItems,
DialogInterface.OnMultiChoiceClickListener multiListener,
DialogInterface.OnClickListener listener){
builder = getBuilder(context);
builder.setTitle(title)
.setMultiChoiceItems(items, choiceItems, multiListener)
.setPositiveButton("确定", listener);
return builder.create();
}
public static Dialog getMultiItemDialog(Context context,String title,int items,boolean[] choiceItems,
DialogInterface.OnMultiChoiceClickListener multiListener,
DialogInterface.OnClickListener listener){
builder = getBuilder(context);
builder.setTitle(title)
.setMultiChoiceItems(items, choiceItems, multiListener)
.setPositiveButton("确定", listener);
return builder.create();
}
public static BottomDialog getBottomDialog(Context context, int layoutId, int[] item, int position,
BottomDialog.OnBottomMenuItemClickListener listener){
BottomDialog dialog = new BottomDialog(context,layoutId,item,position);
dialog.setOnBottomMenuItemClickListener(listener);
return dialog;
}
}
在MainActivity.java中调用
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button button1,dialog_title,single_btn;
Button singleChoose,multi;
Button bottom,center;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
button1 = (Button)findViewById(R.id.dialog_progress);
dialog_title = (Button)findViewById(R.id.dialog_message_title);
single_btn = (Button)findViewById(R.id.dialog_message_single);
singleChoose = (Button)findViewById(R.id.dialog_item_single);
multi = (Button)findViewById(R.id.dialog_item_multi);
bottom = (Button)findViewById(R.id.dialog_bottom);
center = (Button)findViewById(R.id.dialog_center);
button1.setOnClickListener(this);
dialog_title.setOnClickListener(this);
single_btn.setOnClickListener(this);
singleChoose.setOnClickListener(this);
multi.setOnClickListener(this);
bottom.setOnClickListener(this);
center.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.dialog_progress:
DialogManager.getProgressDialog(MainActivity.this,"正在加载...").show();
break;
case R.id.dialog_message_title:
DialogManager.getMessageDialog(MainActivity.this,"标题","这是提示信息","确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"确定",Toast.LENGTH_SHORT).show();
}
}).show();
break;
case R.id.dialog_message_single:
DialogManager.getSingleMessageDialog(MainActivity.this,android.R.drawable.ic_dialog_info,"标题","这是单个按钮","确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"确定",Toast.LENGTH_SHORT).show();
}
}).show();
break;
case R.id.dialog_item_single:
final int[] position = {0};
final String[] item = new String[]{"选项1","选项2","选项3"};
DialogManager.getSingleItemDialog(MainActivity.this, "单选列表", item, position[0],
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
position[0] = which;
}
},
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, item[position[0]], Toast.LENGTH_SHORT).show();
}
}).show();
break;
case R.id.dialog_item_multi:
final String[] items = new String[]{"选项1","选项2","选项3"};
boolean[] choose = new boolean[]{false,false,false};
DialogManager.getMultiItemDialog(MainActivity.this, "多选列表", items, choose,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(MainActivity.this, items[which]+isChecked, Toast.LENGTH_SHORT).show();
}
},
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"确定",Toast.LENGTH_SHORT).show();
}
}).show();
break;
case R.id.dialog_bottom:
BottomDialog dialog = DialogManager.getBottomDialog(MainActivity.this, R.layout.input_dialog,
new int[]{R.id.input_dialog_sure, R.id.input_dialog_cancel}, Gravity.BOTTOM,
new BottomDialog.OnBottomMenuItemClickListener() {
@Override
public void onBottomMenuItemClick(BottomDialog dialog, View view) {
switch (view.getId()){
case R.id.input_dialog_sure:
EditText editText1 = dialog.getView(R.id.input_dialog_edit);
String content = editText1.getText().toString();
Toast.makeText(MainActivity.this,content,Toast.LENGTH_SHORT).show();
}
}
});
dialog.show();
break;
case R.id.dialog_center:
BottomDialog center_dialog = DialogManager.getBottomDialog(MainActivity.this, R.layout.input_dialog,
new int[]{R.id.input_dialog_sure, R.id.input_dialog_cancel}, Gravity.CENTER,
new BottomDialog.OnBottomMenuItemClickListener() {
@Override
public void onBottomMenuItemClick(BottomDialog dialog, View view) {
switch (view.getId()){
case R.id.input_dialog_sure:
EditText editText1 = dialog.getView(R.id.input_dialog_edit);
String content = editText1.getText().toString();
Toast.makeText(MainActivity.this,content,Toast.LENGTH_SHORT).show();
}
}
});
center_dialog.show();
break;
}
}
}
源码下载地址:源码下载