프로그래밍/Android

AlertDialog 만들기

chance 2009. 11. 6. 00:36

 

AlertDialog 는 일반적인 알림 대화상자에서 가장 범용적으로 사용되는 폼을 가지고 있다.

요녀석은 다른 다이얼로그와 조금 다르게 생성하고, 설정을 해야 하는데..

 

AlertDialog.Builder 를 통해 레이아웃을 붙이거나 설정을 조절할 수 있다.

 

1. 레이아웃 생성

다이얼로그로 사용할 레이아웃을 하나 만든다..

 

기타 원하는 View를 추가한다.

 

2. AlertDialog

 

// AlertDialog 객체 선언

AlertDialog.Builder builder;

AlertDialog alertDialog;

 

// Context 얻고, 해당 컨텐스트의 레이아웃 정보 얻기

Context context = getApplicationContext();

LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER);

 

// 레이아웃 설정

View layout = inflater.inflate( R.layout.customDialog , (ViewGroup) findViewById( R.id.layoutRoot ) );

 

// 각 뷰 구성

 

// AlertDialog.Builder 생성

builder = new AlertDialog.Builder( context );

builder.setView( layout );

 

// 다이얼로그 생성

alertDialog = builder.create();