반응형

출처: http://arabiannight.tistory.com/entry/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9CAndroid-%EA%B8%B0%EB%B3%B8-Dialog-inflate-Dialog

 

AlertDialog를 사용한 기본 Dialog 예제 입니다.


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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package arabiannight.tistory.com.simpledialog;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class TestSimpleDialogActivity extends Activity implements OnClickListener {
     
    private AlertDialog mDialog = null;
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        setLayout();
         
    }
     
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_base:
            mDialog = createDialog();
            mDialog.show();
            break;
             
        case R.id.btn_inflate:
            mDialog = createInflaterDialog();
            mDialog.show();
            break
 
        default:
            break;
        }
    }
     
     
    /**
     * base 다이얼로그
     * @return ab
     */
    private AlertDialog createDialog() {
        AlertDialog.Builder ab = new AlertDialog.Builder(this);
        ab.setTitle("Title");
        ab.setMessage("내용");
        ab.setCancelable(false);
        ab.setIcon(getResources().getDrawable(R.drawable.ic_launcher));
          
        ab.setPositiveButton("확인", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                setDismiss(mDialog);
            }
        });
          
        ab.setNegativeButton("취소", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                setDismiss(mDialog);
            }
        });
          
        return ab.create();
    }
     
     
    /**
     * Infalter 다이얼로그
     * @return ab
     */
    private AlertDialog createInflaterDialog() {
        final View innerView = getLayoutInflater().inflate(R.layout.dialog_layout, null);
        AlertDialog.Builder ab = new AlertDialog.Builder(this);
        ab.setTitle("Title");
        ab.setView(innerView);
          
        ab.setPositiveButton("확인", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                setDismiss(mDialog);
            }
        });
          
        ab.setNegativeButton("취소", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                setDismiss(mDialog);
            }
        });
          
        return ab.create();
    }
     
    /**
     * 다이얼로그 종료
     * @param dialog
     */
    private void setDismiss(Dialog dialog){
        if(dialog != null && dialog.isShowing())
            dialog.dismiss();
    }
 
 
    /*
     * Layout
     */
    private Button baseButton = null;
    private Button inflateButton = null;
     
    private void setLayout(){
        baseButton = (Button) findViewById(R.id.btn_base);
        inflateButton = (Button) findViewById(R.id.btn_inflate);
         
        baseButton.setOnClickListener(this);
        inflateButton.setOnClickListener(this);
    }
}





파일첨부 :


TestSimpleDialog.zip


스크린샷 : 


반응형

+ Recent posts