提示框:Dialog的用法 与 封装

用法 :
第一步,创建一个弹出框对象:

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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值