다중선택을 위한 다이얼로그 창을 띄운다. 리스트에는 제목과 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 ); |
05 |
ab.setSingleChoiceItems(items, 0 , |
06 |
new DialogInterface.OnClickListener() { |
07 |
public void onClick(DialogInterface dialog, int whichButton) { |
10 |
}).setPositiveButton( "Ok" , |
11 |
new DialogInterface.OnClickListener() { |
12 |
public void onClick(DialogInterface dialog, int whichButton) { |
15 |
}).setNegativeButton( "Cancel" , |
16 |
new DialogInterface.OnClickListener() { |
17 |
public void onClick(DialogInterface dialog, int whichButton) { |
다이얼로그 창을 띄울 때 공지나 경고성 창이라면 내용이 들어갈것이다. 이 내용을
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> |
07 |
HTML 이 제대로 표현되는지 본다.")); |
08 |
ab.setPositiveButton( "ok" , null ); |
(3) 프로그레시브(Progress) 다이얼로그 구현
|
안드로이드에서 프로그레시브바를 구현할수 있다. 이것을 구현하기 위한 클래스는
ProgressDialog 를 사용해야 한다. 다이얼로그 창의 중지는 ProgressDialog 의
dismiss 를 쓰면된다.
1 |
private void DialogProgress(){ |
2 |
ProgressDialog dialog = ProgressDialog.show(DialogSample. this , "" , |
3 |
"잠시만 기다려 주세요 ..." , true ); |
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, - 1 , new DialogInterface.OnClickListener() { |
07 |
public void onClick(DialogInterface dialog, int item) { |
08 |
Toast.makeText(getApplicationContext(), "Phone Model = " +PhoneModels[item], Toast.LENGTH_SHORT).show(); |
12 |
AlertDialog alert = alt_bld.create(); |
(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) { |
09 |
}).setNegativeButton( "No" , |
10 |
new DialogInterface.OnClickListener() { |
11 |
public void onClick(DialogInterface dialog, int id) { |
16 |
AlertDialog alert = alt_bld.create(); |
18 |
alert.setTitle( "Title" ); |
20 |
alert.setIcon(R.drawable.icon); |
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) |
10 |
TimePickerDialog alert = new TimePickerDialog( this , |
11 |
mTimeSetListener, 0 , 0 , false ); |
(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); |
07 |
DatePickerDialog.OnDateSetListener mDateSetListener = |
08 |
new DatePickerDialog.OnDateSetListener() { |
10 |
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { |
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(); |
17 |
DatePickerDialog alert = new DatePickerDialog( this , mDateSetListener, |
aaaa