์์ฝ
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()๊ฐ ์์ฑ๋ ๊ฒฝ์ฐ๋ค์ด ์ข ์ข ์๋ ๊ฒ์ ํ์ธํ๋ค. ์คํ์ค๋ฒํ๋ก์ฐ ๋งน์ ๊ธ๋ฌผ.
'๋น ๊ตฌ๋ฉ ์ฑ์ฐ๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android]ViewPager2 infinite auto scrolling (0) | 2021.08.11 |
---|---|
[Android]Glide fitXY (0) | 2021.08.11 |
[์ํธํ์จ์ด ์์ง๋์ด๋ง] Software Engineering at Google ์ฝ๊ณ ๊ธฐ๋ก _ Chapter 1, 2 (0) | 2021.07.29 |
[Android] Types as Sets / ์งํฉ์ผ๋ก์์ ํ์ ๋ค(UI ์ํ) (0) | 2021.07.27 |
[Kotlin Coroutine]How to make sense of Kotlin coroutines ์ดํด (0) | 2021.07.15 |