Please Enable JavaScript!
Gon[ Enable JavaScript ]

안드로이드(Android) showDialog(int) deprecated 다시 구현하기

안드로이드 개발
반응형

안드로이드(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;
}

 

 

안드로이드(Android) showDialog(int) deprecated 다시 구현하기

다음은 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) showDialog(int) deprecated 다시 구현하기

 

반응형
Posted by 녹두장군1
,