안드로이드(android) 다이얼로그 종류별 구현 방법

|

(1) 여러 개의 멀티선택 옵션으로 표현해 주기

 

다중선택을 위한 다이얼로그 창을 띄운다리스트에는 제목과 radio button

이 있다. AlertDialog.Builder 클래스로 구현하며 리스트중 특정행을 클릭했을 때

이벤트는 setSingleChoiceItems 에 등록한다. Ok 버튼클릭 이벤트는

setPositiveButton , Cancel 버튼클릭 이벤트는 setNegativeButton 에 등록하고

기능을 구현하면 된다.


01 private void DialogSelectOption() {
02     final String items[] = { "item1""item2""item3" };
03     AlertDialog.Builder ab = new AlertDialog.Builder(DialogSample.this);
04     ab.setTitle("Title");
05     ab.setSingleChoiceItems(items, 0,
06         new DialogInterface.OnClickListener() {
07         public void onClick(DialogInterface dialog, int whichButton) {
08             // 각 리스트를 선택했을때
09         }
10         }).setPositiveButton("Ok",
11         new DialogInterface.OnClickListener() {
12         public void onClick(DialogInterface dialog, int whichButton) {
13             // OK 버튼 클릭시 , 여기서 선택한 값을 메인 Activity 로 넘기면 된다.
14         }
15         }).setNegativeButton("Cancel",
16         new DialogInterface.OnClickListener() {
17         public void onClick(DialogInterface dialog, int whichButton) {
18             // Cancel 버튼 클릭시
19         }
20         });
21     ab.show();
22 }

 

(2) html 로 구현한 text 넣기

 

다이얼로그 창을 띄울 때 공지나 경고성 창이라면 내용이 들어갈것이다이 내용을

HTML 태그를 적용해 넣을 수가 있다색깔은 제대로 바뀌는 것 같은데 <strong>, <b>

등 글자에 대한 bold 처리가 한거나 안한거나 똑같이 보여서 제대로 표현되는지는

좀더 테스트 해봐야할것같다.

01 private void DialogHtmlView(){
02 AlertDialog.Builder ab=new AlertDialog.Builder(DialogSample.this);
03     ab.setMessage(Html.fromHtml("<strong><font color="#ff0000"> " +
04         "Html 표현여부 " +"</font></strong><br>
05  
06  
07 HTML 이 제대로 표현되는지 본다."));
08         ab.setPositiveButton("ok"null);
09         ab.show();
10 }

 

(3) 프로그레시브(Progress) 다이얼로그 구현

 

안드로이드에서 프로그레시브바를 구현할수 있다이것을 구현하기 위한 클래스는

ProgressDialog 를 사용해야 한다다이얼로그 창의 중지는 ProgressDialog 

dismiss 를 쓰면된다.

1 private void DialogProgress(){
2        ProgressDialog dialog = ProgressDialog.show(DialogSample.this"",
3                         "잠시만 기다려 주세요 ..."true);
4       // 창을 내린다.
5       // dialog.dismiss();
6 }
aaa

 

(4) Radio 버튼을 포함한 다중선택 다이얼로그 창

 

1번 예제와 비슷하게 Radio 버튼이 추가되어있는 다중선택 다이얼로그 창이다.

차이가 있다면 창 타이틀에 setIcon 을 써서 아이콘을 집어넣은것과

선택한 행 번호를 알아와 Toast 로 화면에 문자열을 표시해주는 내용이다.

창을 닫고 싶다면 onclick 함수에 넘어온 DialogInterface 객체로 cancel 함수를

호출해 닫으면 된다.

01 private void DialogRadio(){
02 final CharSequence[] PhoneModels = {"iPhone""Nokia""Android"};
03         AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
04         alt_bld.setIcon(R.drawable.icon);
05         alt_bld.setTitle("Select a Phone Model");
06         alt_bld.setSingleChoiceItems(PhoneModels, -1newDialogInterface.OnClickListener() {
07             public void onClick(DialogInterface dialog, int item) {
08                 Toast.makeText(getApplicationContext(), "Phone Model = "+PhoneModels[item], Toast.LENGTH_SHORT).show();
09                 // dialog.cancel();
10             }
11         });
12         AlertDialog alert = alt_bld.create();
13         alert.show();
14 }

 

(5) 선택에 따른 로직구현을 위한 다이얼로그 창 구현

 

예를 들어 Yes/No 중 하나를 선택함으로서 특정 기능을 구현해주고자 할 때

쓰일만한 예제이다샘플에서는 Yes/No 일때를 구분하여 코드를 구현할수

있도록 나누어져 있다우리가 흔히 보는 대표적인 다이얼로그 창일것이다.

01 private void DialogSimple(){
02     AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
03     alt_bld.setMessage("Do you want to close this window ?").setCancelable(
04         false).setPositiveButton("Yes",
05         new DialogInterface.OnClickListener() {
06         public void onClick(DialogInterface dialog, int id) {
07             // Action for 'Yes' Button
08         }
09         }).setNegativeButton("No",
10         new DialogInterface.OnClickListener() {
11         public void onClick(DialogInterface dialog, int id) {
12             // Action for 'NO' Button
13             dialog.cancel();
14         }
15         });
16     AlertDialog alert = alt_bld.create();
17     // Title for AlertDialog
18     alert.setTitle("Title");
19     // Icon for AlertDialog
20     alert.setIcon(R.drawable.icon);
21     alert.show();
22 }
aaaa

 

(6) Time Picker 시간선택 컨트롤을 다이얼로그에 구현

 

Time Picker 컨트롤을 다이얼로그 창에 구현한 예제이다그리고 다이얼로그에

구현되어있는 Time Picker 에서 선택한 값을 부모 화면에서 받아 그 값을 Toast

로 보여준다
01 private void DialogTimePicker(){
02     TimePickerDialog.OnTimeSetListener mTimeSetListener =
03     new TimePickerDialog.OnTimeSetListener() {
04         public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
05             Toast.makeText(DialogSample.this,
06             "Time is=" + hourOfDay + ":" + minute, Toast.LENGTH_SHORT)
07             .show();
08         }
09     };
10     TimePickerDialog alert = new TimePickerDialog(this,
11 mTimeSetListener, 00false);
12     alert.show();
13 }

 

(7) Date Picker 날짜선택 컨트롤을 다이얼로그에 구현

 

Date Picker 컨트롤을 다이얼로그 창에 구현한 예제이다그리고 다이얼로그에

구현되어있는 Date Picker 에서 선택한 값을 부모 화면에서 받아 그 값을 Toast

로 보여준다창을 띄우기 전에 현재 날짜정보를 넘겨주고 Date Picker 에서는

그것을 받아 표시해 준다.

01 private void DialogDatePicker(){
02     Calendar c = Calendar.getInstance();
03     int cyear = c.get(Calendar.YEAR);
04     int cmonth = c.get(Calendar.MONTH);
05     int cday = c.get(Calendar.DAY_OF_MONTH);
06      
07     DatePickerDialog.OnDateSetListener mDateSetListener =
08     new DatePickerDialog.OnDateSetListener() {
09     // onDateSet method
10     public void onDateSet(DatePicker view, int year, int monthOfYear, intdayOfMonth) {
11          String date_selected = String.valueOf(monthOfYear+1)+
12                 " /"+String.valueOf(dayOfMonth)+" /"+String.valueOf(year);
13          Toast.makeText(DialogSample.this,
14         "Selected Date is ="+date_selected, Toast.LENGTH_SHORT).show();
15     }
16      };
17      DatePickerDialog alert = new DatePickerDialog(this,  mDateSetListener, 
18      cyear, cmonth, cday);
19      alert.show();
20 }
aaaa

'Android 개발 > Android SDK' 카테고리의 다른 글

Avoiding Memory Leaks  (0) 2011.04.13
Start Service at boot  (0) 2011.04.13
logcat에서 한글사용대체하는 방법  (0) 2011.04.13
intent 기능  (0) 2011.04.13
apk만들기 android package 생성  (0) 2011.04.13
And