티스토리 뷰

AlertDialog.Builder을 이용한 알림창 띄우기




뒤로 가기 버튼을 눌렀을 때 알림창을 이용하여 처리할 수 있다.



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
    @Override
    public void onBackPressed() {
        //Alert로 종료시키기
        AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
        dialog  .setTitle("종료 알림")
                .setMessage("정말 종료하시겠습니까?")
                .setPositiveButton("종료합니다."new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                })
                .setNeutralButton("취소합니다."new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this"취소했습니다.", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("아니요"new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this"종료하지 않습니다.", Toast.LENGTH_SHORT).show();
                    }
                })
                .create().show();
cs






dialog 에서


setTitle 은 알림창 제목


setMessage 는 알림창 내용


setPositiveButton 은 확인 버튼과 버튼을 눌렀을 경우 처리


setNeutralButton 은 취소 버튼과 버튼을 눌렀을 경우의 처리


setNegativeButton 은 아니요 버튼과 버튼을 눌렀을 경우의 처리





결과 화면




댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
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
글 보관함