Android AlertDialog使用

android.support.v7.app.AlertDialog
基本使用

public AlertDialog showDef(Context actCtx,String title,String msg,String yesBtn,String
            neutralBtn,String noBtn,final OnClickBtnListener listener) {
        AlertDialog.Builder b  = new AlertDialog.Builder(actCtx);
        if (!TextUtils.isEmpty(title)) {
            b.setTitle(title);
        }
        if (!TextUtils.isEmpty(msg)) {
            b.setMessage(msg);
        }
        if (!TextUtils.isEmpty(noBtn)) {
            b.setNegativeButton(noBtn,new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog,int which) {
                    if (null != listener) {
                        listener.onNo(dialog, which);
                    }
                    dialog.dismiss();
                }
            });
        }

        if (!TextUtils.isEmpty(yesBtn)) {
            b.setPositiveButton(yesBtn,new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog,int which) {
                    if (null != listener) {
                        listener.onYes(dialog, which);
                    }
                    dialog.dismiss();
                }
            });
        }
        if (!TextUtils.isEmpty(neutralBtn)) {
            b.setNeutralButton(neutralBtn,new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog,int which) {
                    if (null != listener) {
                        listener.onNeutral(dialog, which);
                    }
                    dialog.dismiss();
                }
            });
        }
        return dialog = b.show();
    }

修改button的属性:
注意要在show()后调用,修改才有效

public void setYesBtnFocus() {
        if (null == dialog ) return;
        if (!dialog.isShowing()) return;
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setBackground(dialog.getContext().getResources().getDrawable(R.drawable.item_bg_focused));
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setBackground(null);
        
    }

你可能感兴趣的:(Android AlertDialog使用)