본문 바로가기
Android 개발/android :: 예제

[안드로이드] 기본 알림창 AlertDialog로 팝업 다이얼로그 띄우기

by 독학하는 1인 개발자 2019. 8. 28.

안드로이드 개발 예제

 

Android AlertDialog

 

팝업창 다이얼로그 띄우는 방법

 

초간단 예제

 

 

 

 

 

 

1. AlertDialog란?

 

AlertDialog는 안드로이드에서 기본 제공하는 다이얼로그로

 

별도의 xml 파일 연결이나 커스텀 과정 없이

 

간단한 Java 코드만으로 구현할 수 있다.

 

 

 

 

2. AlertDialog 예제

알림창을 띄우고 싶은 부분에 (예를 들어, 버튼의 OnClickListener 내부)

 

다음 코드를 집어 넣으면 끝난다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
new AlertDialog.Builder(TestActivity.this// TestActivity 부분에는 현재 Activity의 이름 입력.
     .setMessage("AlertDialog 테스트")     // 제목 부분 (직접 작성)
     .setPositiveButton("확인"new DialogInterface.OnClickListener() {      // 버튼1 (직접 작성)
          public void onClick(DialogInterface dialog, int which){              
               Toast.makeText(getApplicationContext(), "확인 누름", Toast.LENGTH_SHORT).show(); // 실행할 코드
          }
     })
     .setNegativeButton("취소"new DialogInterface.OnClickListener() {     // 버튼2 (직접 작성)
          public void onClick(DialogInterface dialog, int which){
               Toast.makeText(getApplicationContext(), "취소 누름", Toast.LENGTH_SHORT).show(); // 실행할 코드 
          }
     })
     .show();
cs

 

 

 

 

3. 실행 화면

 

 

 

댓글