用法 :
第一步,创建一个弹出框对象:
AlertDialog.Builder builder=new AlertDialog.Builder(this);
第二步,设置相关属性:
builder.setTitle("提示");//标题
builder.setMessage(getString(R.string.change_pwd_success));//身体部分显示的信息,即要提示的内容
第三步,确定按钮,取消按钮,可按需取舍:
//确定按钮
builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
ChangePwdActivity.this.finish();
}
});
//取消按钮
builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
第四步,调用方法
builder.create().show();
完整代码:
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setMessage(getString(R.string.change_pwd_success));
builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
ChangePwdActivity.this.finish();
}
});
builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
封装:
DialogUtil.java完整代码:
package com.hanway.oa.util;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;
import com.hanway.oa.activity.ShitangActivity;
import android.app.Activity;
public class DialogUtil {
// 定义一个显示消息的对话框
public static void showDialog(final Context ctx, String msg,
boolean closeSelf) {
// 创建一个AlertDialog.Builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(ctx).setMessage(
msg).setCancelable(false);
if (closeSelf) {
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 结束当前Activity
((Activity) ctx).finish();
}
});
} else {
builder.setPositiveButton("确定", null);
}
builder.create().show();
}
public static void showListDialog(final Context ctx,String title,String[] items,Boolean closeSelf,OnClickListener listener) {
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle(title);
builder.setItems(items,listener );
builder.show();
}
// 定义一个显示指定组件的对话框
public static void showDialog(Context ctx, View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(ctx)
.setView(view).setCancelable(false)
.setPositiveButton("确定", null);
builder.create().show();
}
public static void showDialog(final Context ctx, String msg,
OnClickListener listener) {
// 创建一个AlertDialog.Builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(ctx).setMessage(
msg).setCancelable(false);
builder.setPositiveButton("确定", listener).show();
}
public static void showSelectDialog(final Context ctx, String msg,Boolean useAble,
OnClickListener listener) {
// 创建一个AlertDialog.Builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(ctx).setMessage(
msg);
builder.setTitle("提示");
if (useAble) {
builder.setPositiveButton("确定", listener);
}
builder.setNeutralButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
builder.show();
}
public static AlertDialog showDialog(final Context ctx, String msg,
OnClickListener positivelistener, OnClickListener negetivelistener) {
// 创建一个AlertDialog.Builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(ctx).setMessage(
msg).setCancelable(false);
return builder.setPositiveButton("确定", positivelistener)
.setNegativeButton("取消", negetivelistener).show();
}
public static void showProgessDialog(final Context ctx, String msg,
OnClickListener positivelistener, OnClickListener negetivelistener) {
// 创建一个AlertDialog.Builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(ctx).setMessage(
msg).setCancelable(false);
builder.setPositiveButton("确定", positivelistener)
.setNegativeButton("取消", negetivelistener).show();
}
public static void showToast(String msg, Context context) {
Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}