Dialog with Callback





CommonDialog.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class CommonDialog extends Dialog {
 
    Context context;
    CommonDialogCallback callback;
 
    // 내용
    String title;
    String message;
    String positiveButtonName;
    String negativeButtonName;
 
    public CommonDialog(Context context) {
        super(context);
    }
 
    public CommonDialog(Context context,
                        CommonDialogCallback callback,
                        String title,
                        String message,
                        String positiveButtonName,
                        String negativeButtonName)
    {
        super(context);
 
        this.context = context;
        this.callback = callback;
        this.title = title;
        this.message = message;
        this.positiveButtonName = positiveButtonName;
        this.negativeButtonName = negativeButtonName;
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        AlertDialog.Builder builder;
        builder = new AlertDialog.Builder(context, R.style.Dialog);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(positiveButtonName, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                callback.positiveButtonClick();
            }
        });
        builder.setNegativeButton(negativeButtonName, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                callback.negativeButtonClick();
            }
        });
 
        AppCompatDialog dialog = builder.create();
        dialog.show();
    }
}
cs



CommonDialogCallback.java


1
2
3
4
5
6
7
8
public interface CommonDialogCallback
{
    // positiveButton
    void positiveButtonClick();
 
    // negativeButton
    void negativeButtonClick();
}
cs




구현부


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
 
        Log.d(CommonDefine.TAG, "===== Activity_Intro onCreate =====");
 
        initLayout ();
 
        CommonDialog commonDialog = new CommonDialog(
                this,
                dialogUtilsCallback,
                "TITLE",
                "MESSAGE",
                "POSITIVE",
                "NEGATIVE");
        commonDialog.create();
    }
 
    // 다이얼로그 버튼 클릭 콜백
    CommonDialogCallback dialogUtilsCallback = new CommonDialogCallback() {
        @Override
        public void positiveButtonClick() {
            Log.d(CommonDefine.TAG, "===== POSITIVE =====");
        }
 
        @Override
        public void negativeButtonClick() {
            Log.d(CommonDefine.TAG, "===== NEGATIVE =====");
        }
    };
cs


'Developer > Android' 카테고리의 다른 글

안드로이드 6.0 이상 UsageStatsManager를 이용해 Process list 받아오기  (0) 2017.09.13
AppCompatButton with change Color  (0) 2017.07.25
Dialog with Callback  (0) 2017.07.24
running handler with Main Looper  (0) 2017.06.09
MaxHeightScrollView  (0) 2016.08.09
MaxheightListView  (0) 2016.08.09

이 글을 공유하기


댓글(0)

Designed by CMSFactory.NET