사용자 정의 대화 상자 Dialog

4703 단어 자바androidxml
알림 대화 상자:
레이아웃 파일:alertdialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="150dip"
	android:padding="10dip"
	android:background="@color/dialog_bg"
	>
    <LinearLayout android:id="@+id/title_layout"
        android:orientation="vertical"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">
    	<TextView android:id="@+id/title_text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:layout_gravity="center_horizontal"
              android:text="    "
              android:textColor="#FFF"/>
    </LinearLayout>
    <LinearLayout android:id="@+id/bottom_layout"
        android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
	    <Button android:id="@+id/button_yes"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="                      "
             android:gravity="center" />
	    <Button android:id="@+id/button_no"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="                     "
             android:gravity="center" />
    </LinearLayout>
</RelativeLayout>

사용자 정의 클래스 AlertDialog.java
public class AlertDialog extends Dialog implements android.view.View.OnClickListener
{
	OnClickListener onClickListener;
	Button yes;
	Button no;
	
	public AlertDialog(Context context)
	{
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getWindow().findViewById(android.R.id.title).setVisibility(View.GONE);//        
		//getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.alertdialog);
		
		yes = (Button)findViewById(R.id.button_yes);
		no = (Button)findViewById(R.id.button_no);
		yes.setOnClickListener(this);
		no.setOnClickListener(this);
	}

	@Override
	protected void onStop()
	{
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	public void onClick(View v)
	{
		switch(v.getId()) {
		case R.id.button_yes:
			onClickListener.onClick(onClickListener.BUTTON_YES);
			break;
		case R.id.button_no:
			onClickListener.onClick(onClickListener.BUTTON_NO);
			break;
		}
	}
	
	public void setOnClickListener(OnClickListener onClickListener)
	{
		this.onClickListener = onClickListener;
	}

	public interface OnClickListener {
		public static final int BUTTON_YES = 0;
		public static final int BUTTON_NO = 1;
		
		void onClick(int type);
	}
}

호출 코드:
protected Dialog onCreateDialog(int id)
	{
		switch(id) {
		case ALERT_DIALOG:
			AlertDialog dialog = new AlertDialog(ContactsActivity.this);
			dialog.setOnClickListener(new AlertDialog.OnClickListener()
			{
				@Override
				public void onClick(int type)
				{
					switch(type) {
					case AlertDialog.OnClickListener.BUTTON_YES:
						dismissDialog(ALERT_DIALOG);
						//showDialog(PROGRESS_DIALOG);
						backupContacts();
						break;
					case AlertDialog.OnClickListener.BUTTON_NO:
						dismissDialog(ALERT_DIALOG);
						break;
					}
				}
			});
			dialog.setCancelable(false);// back       
			return dialog;
		case PROGRESS_DIALOG:
			
		}
		return super.onCreateDialog(id);
	}

좋은 웹페이지 즐겨찾기