Android :界面优美的自定义弹出框———ConfirmDialog

    效果图:

Android :界面优美的自定义弹出框———ConfirmDialog_第1张图片

    自定义View:

public class ConfirmDialogQuit extends Dialog {
    private Context context;
    private TextView titleTv,contentTv;
    private View okBtn,cancelBtn;
    private OnDialogClickListener dialogClickListener;

    public ConfirmDialogQuit(Context context) {
        super(context);
        this.context = context;
        initalize();
    }

    //初始化View
    private void initalize() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.confirm_dialog_quit, null);
        setContentView(view);
        initWindow();

        titleTv = (TextView) findViewById(R.id.title_name);
        contentTv = (TextView) findViewById(R.id.text_view);
        okBtn = findViewById(R.id.btn_ok);
        cancelBtn = findViewById(R.id.btn_cancel);
        okBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
                if(dialogClickListener != null){
                    dialogClickListener.onOKClick();
                }
            }
        });
        cancelBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
                if(dialogClickListener != null){
                    dialogClickListener.onCancelClick();
                }
            }
        });
    }

    /**
     *添加黑色半透明背景
     */
    private void initWindow() {
        Window dialogWindow = getWindow();
        dialogWindow.setBackgroundDrawable(new ColorDrawable(0));//设置window背景
        dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);//设置输入法显示模式
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics();//获取屏幕尺寸
        lp.width = (int) (d.widthPixels * 0.8); //宽度为屏幕80%
        lp.gravity = Gravity.CENTER;  //中央居中
        dialogWindow.setAttributes(lp);
    }

    public void setOnDialogClickListener(OnDialogClickListener clickListener){
        dialogClickListener = clickListener;
    }

    /**
     *添加按钮点击事件
     */
    public interface OnDialogClickListener{
        void onOKClick();
        void onCancelClick();
    }
}

布局文件:



    

        
    

    

        

        

        

            
            

 布局添加圆角,颜色:

   在drawable中新建:



    
    
    
    

实现:

 ConfirmDialogQuit confirmDialog = new ConfirmDialogQuit(getActivity());
                    confirmDialog.setOnDialogClickListener(new ConfirmDialogQuit.OnDialogClickListener() {
                        @Override
                        public void onOKClick() {
                          
                            confirmDialog.dismiss();
                        }

                        @Override
                        public void onCancelClick() {
                            confirmDialog.dismiss();
                        }
                    });
                    confirmDialog.setCancelable(false);//点击空白处不消失
                    confirmDialog.show();


                }

 完成!

你可能感兴趣的:(Android :界面优美的自定义弹出框———ConfirmDialog)