`
zxl_ong
  • 浏览: 126653 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

自定义对话框Dialog

阅读更多
提醒对话框:
布局文件: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);
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics