반응형
안드로이드(Android) showDialog(int) deprecated 다시 구현하기 |
개발환경 : window 7 64bit, Eclipse Mars, Android 4.2.2 |
API 레벨 13 부터 사용하지 않는 함수가 되었습니다. 이것을 다시 구현해 보도록 하겠습니다. 샘플은 2가지 입니다. |
먼저 이전 방식인 deprecated 된 소스를 보도록
하겠습니다. Activity 가 생성되기 전에 onCreateDialog()
onPrepareDialog() 함수를 사용해 다이얼로그를 만든후
showDialog(ID_JPGDIALOG) 를 사용해 호출하였습니다.
그러니까 두 함수를 사용해 다양한 다이얼로그를
등록해서 switch 이용해 원하는 것을 선택해서 띄운것이죠.
@Override protected Dialog onCreateDialog(int id) { dlg = null; switch (id) { case ID_JPGDIALOG: Context mContext = this; dlg = new Dialog(mContext); dlg.setContentView(R.layout.dialog_select_image_view); content = (TextView) dlg.findViewById(R.id.dlgImageName); Button okDialogButton = (Button) dlg.findViewById(R.id.btnOk); okDialogButton.setOnClickListener(okDialogButtonOnClickListener); break; default: break; } return dlg; } @Override protected void onPrepareDialog(int id, Dialog dialog) { switch (id) { case ID_JPGDIALOG: dialog.setTitle("[다이얼로그 제목]"); content.setText("다이얼로그 내용"); break; default: break; } }
위의 소스를 바꾸는 간단한 방법은 다음과 같습니다.
Dialog 객체를 리턴하는 함수를 하나 만들고 인수로
어떤 다이얼로그를 선택할지 넘기게 만듭니다.
그리고 메인에서 createDialog(ID_JPGDIALOG).show() ;
호출하면 끝입니다.
protected Dialog createdDialog(int id) { dlg = null; switch (id) { case ID_JPGDIALOG: Context mContext = this; dlg = new Dialog(mContext); dlg.setContentView(R.layout.dialog_select_image_view); content = (TextView) dlg.findViewById(R.id.dlgImageName); Button okDialogButton = (Button) dlg.findViewById(R.id.btnOk); okDialogButton.setOnClickListener(okDialogButtonOnClickListener); break; default: break; } return dlg; }
다음은 AlertDialog.Builder 를 이용해 새로운 다이얼로그를
생성하여 호출하는 방법입니다. AlertDialog.Builder 객체를
생성한후 show() 함수를 호출해주시면 됩니다.
이것은 첫번째 예제와 달리 레이아웃이 없습니다.
화면을 꾸밀수가 없다는 것이죠.
AlertDialog.Builder alertDialog= new AlertDialog.Builder(SampleCreateDialog.this); alertDialog.setTitle("this is a dialog box "); alertDialog.setPositiveButton("ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(getBaseContext(),"ok ive wrote this 'ok' here" ,Toast.LENGTH_SHORT).show(); } }); alertDialog.setNegativeButton("cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(getBaseContext(), "cancel ' comment same as ok'", Toast.LENGTH_SHORT).show(); } }); alertDialog.show();
반응형
'안드로이드 개발' 카테고리의 다른 글
안드로이드(Android) 간단한 RatingBar 사용예제 (0) | 2014.11.18 |
---|---|
안드로이드(Android) 이미지에서의 Exif GPS 정보를 GeoPoint 바꾸기 (6) | 2014.11.17 |
안드로이드(Android) 문의 – Dialog 클래스를 이용하여 다이얼로그 계산기 올리기 (0) | 2014.11.16 |
안드로이드(Android) 이미지뷰에서 Exif 정보 다이얼로그 띄우기 (1) | 2014.11.15 |
안드로이드(Android) 파일 브라우저에서 이미지 보여주기 (1) | 2014.11.13 |
안드로이드(Android) 파일브라우저에서 이미지 클릭시 Exif 정보 출력하기 (0) | 2014.11.12 |
안드로이드(Android) 간단한 파일 브라우저 만들기 (0) | 2014.11.11 |
안드로이드(Android) 지니모션(GenyMotion) 에뮬레이터 파일 옮기기 (0) | 2014.11.10 |