안드로이드에서는 AlertDialog라는 다이얼로그를 제공하는데..
PositiveButton, NegativeButton, NeutralButton 이렇게
세 가지의 버튼을 지원한다.
아무래도 용어도 생소할뿐더러
MS 형식에 익숙해져 있어서 새롭게 만들어보았다.
만들면서도 괜히하는 것은 아닌가라는 생각이 들지만..
이렇게 하면서 배우는 것도 있을테니까..
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
1. 우선 버튼의 종류와 그 버튼을 클릭했을 때의 리턴값을 정의한다.
종류는 OK, OKCANCEL, YESNO, YESNOCANCEL, 임의의 버튼1개, 임의의 버튼2개, 임의의 버튼3개
이렇게 정했다.
public final static int OK = 1; // button type, return type
public final static int CANCEL = 2; // return type
public final static int YES = 3; // return type
public final static int NO = 4; // return type
public final static int OKCANCEL = 5; // button type
public final static int YESNO = 6; // button type
public final static int YESNOCANCEL = 7; // button type
public final static int ONE_BUTTON = 8; // button type
public final static int TWO_BUTTON = 9; // button type
public final static int THREE_BUTTON = 10; // button type
public final static int BUTTON1 = 11; // return type;
public final static int BUTTON2 = 12; // return type;
public final static int BUTTON3 = 13; // return type;
2. 다이얼로그의 아이콘을 표시하기 위해 상수를 지정한다.
여기서는 INFO와 ALERT 두 가지를 지정했다
(왜냐면 구글에서 이 두가지의 아이콘만 제공한다)
public final static int INFO = 1;
public final static int ALERT = 2;
3. 클릭 이벤트를 처리할 리스너를 만든다
protected OnClickListener mOnClickListener;
4. setButtonText 메소드를 통해서 임의의 버튼의 텍스를 지정한다.(전체소스 참조)
5. show 메소드를 통해서 실제로 다이얼로그를 표시한다.
show 메소드의 파라미터는 타이틀, 메시지, 버튼타입, 아이콘 타입인데..
오버라이드를 해서 타이틀과 메시지만 입력했을 경우는
INFO 아이콘에 확인 버튼 하나만 나오도록 했다.
그리고 다이얼로그 종류에 따라서 버튼을 클릭했을 때
해당하는 버튼값이 리턴되도록 지정한다.
## 주의할 점은 안드로이드는 MS의 다이얼로그와 달리 Modal 형식이 아니어서
사용자가 버튼을 클릭할 때까지 기다려주지 않는다.
그래서 클릭 이벤트를 만들고 클릭했을 때에
다음 동작을 처리해야 한다.
예)
SimpleDialog dialog = new SimpleDialog(context);
dialog.show("제목", "메시지 내용");
dialog.setOnClickListener( new SimpleDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 처리할 내용 구현
}
});
-- 전체소스 --
public class SimpleDialog {
public final static int OK = 1; // button type, return type
public final static int CANCEL = 2; // return type
public final static int YES = 3; // return type
public final static int NO = 4; // return type
public final static int OKCANCEL = 5; // button type
public final static int YESNO = 6; // button type
public final static int YESNOCANCEL = 7; // button type
public final static int ONE_BUTTON = 8; // button type
public final static int TWO_BUTTON = 9; // button type
public final static int THREE_BUTTON = 10; // button type
public final static int BUTTON1 = 11; // return type;
public final static int BUTTON2 = 12; // return type;
public final static int BUTTON3 = 13; // return type;
public final static int INFO = 1;
public final static int ALERT = 2;
protected OnClickListener mOnClickListener;
private Context mContext;
private int mResult = -1;
private String mButton1Text = "";
private String mButton2Text = "";
private String mButton3Text = "";
public SimpleDialog(Context context) {
// TODO Auto-generated constructor stub
mContext = context;
}
public void setButtonText(int button, int caption) {
String strCaption = mContext.getResources().getString(caption);
setButtonText(button, strCaption);
}
public void setButtonText(int button, String caption) {
switch (button) {
case 1:
case BUTTON1:
mButton1Text = caption;
break;
case 2:
case BUTTON2:
mButton2Text = caption;
break;
case 3:
case BUTTON3:
mButton3Text = caption;
break;
}
}
public int getResult() {
return mResult;
}
public int show(int title, int msg) {
String strTitle = mContext.getResources().getString(title);
String strMsg = mContext.getResources().getString(msg);
return show(strTitle, strMsg, OK, INFO);
}
public int show(int title, String msg) {
String strTitle = mContext.getResources().getString(title);
return show(strTitle, msg, OK, INFO);
}
public int show(String title, int msg) {
String strMsg = mContext.getResources().getString(msg);
return show(title, strMsg, OK, INFO);
}
public int show(String title, String msg) {
return show(title, msg, OK, INFO);
}
public int show(int title, int msg, int buttonType, int iconType) {
String strTitle = mContext.getResources().getString(title);
String strMsg = mContext.getResources().getString(msg);
return show(strTitle, strMsg, buttonType, iconType);
}
public int show(int title, String msg, int buttonType, int iconType) {
String strTitle = mContext.getResources().getString(title);
return show(strTitle, msg, buttonType, iconType);
}
public int show(String title, int msg, int buttonType, int iconType) {
String strMsg = mContext.getResources().getString(msg);
return show(title, strMsg, buttonType, iconType);
}
public int show(String title, String msg, int buttonType, int iconType) {
Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage(msg).setCancelable(false);
if (buttonType == OK || buttonType == OKCANCEL) {
builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
mResult = OK;
performClick(dialog, OK);
}
});
}
if (buttonType == OKCANCEL) {
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
mResult = CANCEL;
performClick(dialog, CANCEL);
}
});
}
if (buttonType == YESNO || buttonType == YESNOCANCEL) {
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
mResult = YES;
performClick(dialog, YES);
}
});
builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
mResult = NO;
performClick(dialog, NO);
}
});
}
if (buttonType == YESNOCANCEL) {
builder.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
mResult = CANCEL;
performClick(dialog, CANCEL);
}
});
}
if (buttonType == ONE_BUTTON || buttonType == TWO_BUTTON || buttonType == THREE_BUTTON) {
builder.setPositiveButton(mButton1Text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
mResult = BUTTON1;
performClick(dialog, BUTTON1);
}
});
}
if (buttonType == TWO_BUTTON || buttonType == THREE_BUTTON) {
builder.setNegativeButton(mButton2Text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
mResult = BUTTON2;
performClick(dialog, BUTTON2);
}
});
}
if (buttonType == THREE_BUTTON) {
builder.setNeutralButton(mButton3Text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
mResult = BUTTON3;
performClick(dialog, BUTTON3);
}
});
}
AlertDialog alert = builder.create();
alert.setTitle(title);
if (iconType == INFO)
alert.setIcon(R.drawable.ic_dialog_info);
else if (iconType == ALERT)
alert.setIcon(R.drawable.ic_dialog_alert);
alert.show();
return mResult;
}
public static interface OnClickListener {
public abstract void onClick(android.content.DialogInterface dialog, int which);
}
public void setOnClickListener(OnClickListener l) {
mOnClickListener = l;
}
public boolean performClick(DialogInterface dialog, int id) {
if (mOnClickListener != null) {
mOnClickListener.onClick(dialog, id);
return true;
}
return false;
}
}
댓글 없음:
댓글 쓰기