android CustomDialog

import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class CustomDialog {
    public static String TAG = "CustomDialog";

    /**
     * 等待对话框,可以取消
     */
    public static final int DIALOG_THEME_WAIT_CAN_CANCEL = 0;
    public static final int DIALOG_THEME_ONE_BUTTON_CAN_CANCEL = 2;
    public static final int DIALOG_THEME_TWO_BUTTON_CAN_CANCEL = 4;
    /**
     * 等待对话框,不可以取消
     */
    public static final int DIALOG_THEME_WAIT_NOT_CANCEL = 1;
    public static final int DIALOG_THEME_ONE_BUTTON_NOT_CANCEL = 3;
    public static final int DIALOG_THEME_TWO_BUTTON_NOT_CANCEL = 5;

    private int layout;
    private int style;
    private Dialog dialog = null;
    private final Context context;
    private final int theme;
    private OnCustomDialogListener mListener;

    public CustomDialog(Context context, int theme) {
        this.context = context;
        this.theme = theme;
        init(theme);
    }

    private void init(int theme) {
        if (theme % 2==0) {
            style = R.style.cancelDialogTheme;
        } else {
            style = R.style.DialogTheme;
        }
        dialog = new Dialog(context, style) {

            @Override
            protected void onStop() {
                super.onStop();
                if (mListener != null) {
                    mListener.onFinish();
                }
            }

            @Override
            public boolean onKeyDown(int keyCode, KeyEvent event) {
                if (mListener != null) {
                    mListener.onKeyDown();
                }
                return super.onKeyDown(keyCode, event);
            }

        };
        switch (theme) {
            case DIALOG_THEME_TWO_BUTTON_CAN_CANCEL:
            case DIALOG_THEME_TWO_BUTTON_NOT_CANCEL:
                layout = R.layout.dialog_two_btn;
                break;
            case DIALOG_THEME_ONE_BUTTON_CAN_CANCEL:
            case DIALOG_THEME_ONE_BUTTON_NOT_CANCEL:
                layout = R.layout.dialog_one_btn;
                break;
            case DIALOG_THEME_WAIT_CAN_CANCEL:
            case DIALOG_THEME_WAIT_NOT_CANCEL:
                layout = R.layout.window_layout;
                break;
        }
        dialog.setContentView(layout);
    }

    /**
     * 设置提示内容
     * 
     * @param message
     * @return
     */
    public CustomDialog setMessage(String message) {
        if (theme == DIALOG_THEME_WAIT_CAN_CANCEL) {
            return this;
        }
        TextView title = (TextView) dialog.findViewById(R.id.dialog_title);
        title.setText(message);
        return this;
    }

    public CustomDialog setButton(String btn1Str, OnClickListener btn1OnClickListener, String btn2Str,
            final OnClickListener btn2OnClickListener) {
        this.setButton(btn1Str, btn1OnClickListener);
        if (theme != DIALOG_THEME_TWO_BUTTON_CAN_CANCEL && theme != DIALOG_THEME_TWO_BUTTON_NOT_CANCEL) {
            return this;
        }
        TextView btn2 = (TextView) dialog.findViewById(R.id.dialog_btn2);
        btn2.setText(btn2Str);
        btn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                if (btn2OnClickListener != null) {
                    btn2OnClickListener.onClick(view);
                }
            }
        });
        return this;

    }

    public CustomDialog setButton(String btnStr, final OnClickListener btnOnClickListener) {
        if (theme == DIALOG_THEME_WAIT_CAN_CANCEL || theme == DIALOG_THEME_WAIT_NOT_CANCEL) {
            return this;
        }
        TextView btn1 = (TextView) dialog.findViewById(R.id.dialog_btn1);
        btn1.setText(btnStr);
        btn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                if (btnOnClickListener != null) {
                    btnOnClickListener.onClick(view);
                }
            }
        });
        return this;
    }

    public CustomDialog setOnCustomDialogListener(OnCustomDialogListener listener) {
        this.mListener = listener;
        return this;
    }

    public CustomDialog show() {
        try {
            dialog.show();
        } catch (Exception e) {
            LogUtil.e(TAG, "dialog err", e);
        }
        return this;
    }

    public void dismiss() {
        try {
            dialog.dismiss();
        } catch (Exception e) {
            LogUtil.e(TAG, "dialog err", e);
        }
    }

    public void cancel() {
        try {
            dialog.cancel();
        } catch (Exception e) {
            LogUtil.e(TAG, "dialog err", e);
        }
    }

    public boolean isShowing() {
        return dialog.isShowing();
    }

}
public abstract class OnCustomDialogListener {
    public void onFinish() {
    }
    public void onKeyDown() {
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值