๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Android]AlertDialog.Builder- dismiss() ์‚ฌ์šฉ ๋‚จ๋ฐœ

์š”์•ฝ

AlertDialog.Builder๋กœ ๋‹ค์ด์–ผ๋กœ๊ทธ ์ƒ์„ฑ์‹œ,

1. ๋ฒ„ํŠผ์˜ ๋ฆฌ์Šค๋„ˆ ์•ˆ์— ๋‹ค์ด์–ผ๋กœ๊ทธ์˜ dismiss()๋ฅผ ํ˜ธ์ถœํ•  ํ•„์š”์—†๋‹ค.

2. ์บ”์Šฌ ๋ฆฌ์Šค๋„ˆ ์•ˆ์— ๋‹ค์ด์–ผ๋กœ๊ทธ์˜ dismiss()๋ฅผ ํ˜ธ์ถœํ•  ํ•„์š”์—†๋‹ค.


ํšŒ์‚ฌ ํ”„๋กœ์ ํŠธ์˜ ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ค๊ฐ€ ์˜๋ฌธ์ด ์ƒ๊ฒผ๋‹ค. ์ด๋ฒˆ์—๋Š” ํ™•์‹คํžˆ ์•Œ๊ณ  ๋„˜์–ด๊ฐ€์•ผ์ง€ ์‹ถ์–ด์„œ ๊ธฐ๋กํ•œ๋‹ค.

 

๊ธฐ์กด ์ฝ”๋“œ

Dialog = new AlertDialog.Builder(context)
	    .setTitle("ํƒ€์ดํ‹€")
            .setMessage("๋ฉ”์‹œ์ง€")
            .setPositiveButton("ํ™•์ธ", (dialog, which) -> doWork())
            .setNegativeButton("์ทจ์†Œ", (dialog, which) -> dialog.dismiss()) //๋ฌธ์ œ์˜ ๊ตฌ๊ฐ„1
            .setOnCancelListener((dialog) -> dialog.dismiss()); //๋ฌธ์ œ์˜ ๊ตฌ๊ฐ„2
            .create();

 

๋ฌธ์ œ์˜ ๊ตฌ๊ฐ„1 : ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ dismiss()๋ฅผ ๊ตฌ์ง€ ํ˜ธ์ถœํ•ด์•ผ ํ•˜๋Š”๊ฐ€?

1) AlertDialog.java 

        /**
         * Set a listener to be invoked when the negative button of the dialog is pressed.
         * @param textId The resource id of the text to display in the negative button
         * @param listener The {@link DialogInterface.OnClickListener} to use.
         *
         * @return This Builder object to allow for chaining of calls to set methods
         */
        public Builder setNegativeButton(@StringRes int textId, final OnClickListener listener) {
            P.mNegativeButtonText = P.mContext.getText(textId);
            P.mNegativeButtonListener = listener;
            return this;
        }

mNagativeButtonListener๋ฅผ ์ถ”์ ํ•œ๋‹ค.

 

 2) AlertController.java

 

         public void apply(AlertController dialog) {
            ...
            if (mNegativeButtonText != null || mNegativeButtonIcon != null) {
                dialog.setButton(DialogInterface.BUTTON_NEGATIVE, mNegativeButtonText,
                        mNegativeButtonListener, null, mNegativeButtonIcon);
            }
           ...
        }

 

setButton()์„ ์ถ”์ ํ•œ๋‹ค.

 

mNegativeButtonText๋ฅผ ํ†ตํ•ด negative button ๊ฐ์ฒด๋ฅผ ์ถ”์ ํ•œ๋‹ค.

 

3) AlertController.java

private void setupButtons(ViewGroup buttonPanel) {
        ...

        mButtonNegative = buttonPanel.findViewById(android.R.id.button2);
        mButtonNegative.setOnClickListener(mButtonHandler);

        if (TextUtils.isEmpty(mButtonNegativeText) && mButtonNegativeIcon == null) {
            mButtonNegative.setVisibility(View.GONE);
        } else {
            mButtonNegative.setText(mButtonNegativeText);
            if (mButtonNegativeIcon != null) {
                mButtonNegativeIcon.setBounds(0, 0, mButtonIconDimen, mButtonIconDimen);
                mButtonNegative.setCompoundDrawables(mButtonNegativeIcon, null, null, null);
            }
            mButtonNegative.setVisibility(View.VISIBLE);
            whichButtons = whichButtons | BIT_BUTTON_NEGATIVE;
        }

        ...
    }

mButtonNegative๋ผ๋Š” ๊ฐ์ฒด๊ฐ€ ์žˆ์Œ์„ ํ™•์ธํ–ˆ๋‹ค.

์ด ๋ฒ„ํŠผ์˜ ํด๋ฆญ ๋ฆฌ์Šค๋„ˆ ๊ฐ์ฒด์ธ mButtonHandler๋ฅผ ์ถ”์ ํ•œ๋‹ค.

 

4) AlertContoller.java

    private final View.OnClickListener mButtonHandler = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Message m;
            if (v == mButtonPositive && mButtonPositiveMessage != null) {
                m = Message.obtain(mButtonPositiveMessage);
            } else if (v == mButtonNegative && mButtonNegativeMessage != null) {
                m = Message.obtain(mButtonNegativeMessage);
            } else if (v == mButtonNeutral && mButtonNeutralMessage != null) {
                m = Message.obtain(mButtonNeutralMessage);
            } else {
                m = null;
            }

            if (m != null) {
                m.sendToTarget();
            }

            // Post a message so we dismiss after the above handlers are executed
            mHandler.obtainMessage(ButtonHandler.MSG_DISMISS_DIALOG, mDialog)
                    .sendToTarget();
        }
    };

mButtonNegativeMessage๊ฐ€ null์ด ์•„๋‹ ๊ฒฝ์šฐ m์— Message ์ฐธ์กฐ๊ฐ€ ๋œ๋‹ค.

m.sendtoTarget() ์‹คํ–‰ ํ›„, ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ButtonHandler.MSG_DISMISS_DIALOG๋ฅผ ๋‹ด์€ Message๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ํ•œ๋‹ค.

 

mHandler๋Š” ์–ด๋–ค ๊ฐ์ฒด์ธ์ง€ ์ถ”์ ํ•œ๋‹ค.

 

5) AlertController.java

    public AlertController(Context context, AppCompatDialog di, Window window) {
        ...
        mHandler = new ButtonHandler(di);

       ...
    }

ButtonHandler๋Š” ์–ด๋–ค ํด๋ž˜์Šค์ธ์ง€ ์ถ”์ ํ•œ๋‹ค.

 

6) AlertController.java

    private static final class ButtonHandler extends Handler {
        // Button clicks have Message.what as the BUTTON{1,2,3} constant
        private static final int MSG_DISMISS_DIALOG = 1;

        private WeakReference<DialogInterface> mDialog;

        public ButtonHandler(DialogInterface dialog) {
            mDialog = new WeakReference<>(dialog);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case DialogInterface.BUTTON_POSITIVE:
                case DialogInterface.BUTTON_NEGATIVE:
                case DialogInterface.BUTTON_NEUTRAL:
                    ((DialogInterface.OnClickListener) msg.obj).onClick(mDialog.get(), msg.what);
                    break;

                case MSG_DISMISS_DIALOG:
                    ((DialogInterface) msg.obj).dismiss();
            }
        }
    }

MSG_DISMISS_DIALOG ๊ฐ’์„ ๋ฐ›์œผ๋ฉด ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ dismiss()์‹œํ‚จ๋‹ค.

 

๋‹ค์ด์–ผ๋กœ๊ทธ์˜ ๋ฒ„ํŠผ ๋ฆฌ์Šค๋„ˆ์— ๊ตฌ์ง€ ๋‹ค์ด์–ผ๋กœ๊ทธ์˜ dismiss() ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ํด๋ฆญํ•˜๋ฉด dismiss()๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ๊ธฐ๋ณธ์ ์œผ๋กœ ์งœ์—ฌ์ ธ ์žˆ๋‹ค.

 

 

๋ฌธ์ œ์˜ ๊ตฌ๊ฐ„2 : ์บ”์Šฌ ๋ฆฌ์Šค๋„ˆ์— ๊ตฌ์ง€ dismiss()๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•˜๋Š”๊ฐ€

1) Dialog.java

    /**
     * Cancel the dialog.  This is essentially the same as calling {@link #dismiss()}, but it will
     * also call your {@link DialogInterface.OnCancelListener} (if registered).
     */
    @Override
    public void cancel() {
        if (!mCanceled && mCancelMessage != null) {
            mCanceled = true;
            // Obtain a new message so this dialog can be re-used
            Message.obtain(mCancelMessage).sendToTarget();
        }
        dismiss();
    }

 

cancel()์„ ํ˜ธ์ถœํ•˜๋ฉด dismiss()๋ฅผ ์•Œ์•„์„œ ํ˜ธ์ถœํ•ด์ค€๋‹ค. ๋”ฐ๋ผ์„œ OnCancleListsener์— dialog๋ฅผ dismiss()์‹œํ‚ค๋Š” ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

 

์Šคํƒ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ์— ๋‹ต๋ณ€์œผ๋กœ ๋‚˜์˜ค๋Š” ์ฝ”๋“œ๋“ค ์ค‘ negative ๋ฒ„ํŠผ์˜ ๋ฆฌ์Šค๋„ˆ์— dialog.dismiss()๊ฐ€ ์ž‘์„ฑ๋œ ๊ฒฝ์šฐ๋“ค์ด ์ข…์ข… ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ๋‹ค. ์Šคํƒ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ๋งน์‹  ๊ธˆ๋ฌผ.