본문 바로가기
매일코딩/안드로이드

[안드로이드 기초] 다이얼로그창 alert 띄우기

by 인생여희 2017. 8. 18.
반응형

다이얼로그창 alert 띄우기


#화면

#소스

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
package me.happygate.mydiaolog;
 
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    TextView textView;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        textView = (TextView) findViewById(R.id.textView);
 
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
                //버튼이 눌렸을 때 다이얼로그 함수 호출
               showMessge();
            }
        });
    }
 
 
    public void showMessge(){
        
        //다이얼로그 객체 생성
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //속성 지정
        builder.setTitle("안내");
        builder.setMessage("종료 하시겠습니까?");
        //아이콘
        builder.setIcon(android.R.drawable.ic_dialog_alert);
 
        
        //예 버튼 눌렀을 때
        builder.setPositiveButton("예"new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
 
                //텍스트 뷰 객체를 넣어줌..
                Snackbar.make(textView ,"예버튼이 눌렸습니다",Snackbar.LENGTH_SHORT).show();
            }
        });
 
 
        //예 버튼 눌렀을 때
        builder.setNegativeButton("아니오"new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
 
                //텍스트 뷰 객체를 넣어줌..
                Snackbar.make(textView ,"아니오 버튼이 눌렸습니다",Snackbar.LENGTH_SHORT).show();
            }
        });
 
        //만들어주기
        AlertDialog dialog = builder.create();
        dialog.show();
    }
 
}
 
cs


반응형

댓글