仿IOS对话框

显示效果如下:



CustomDialog和系统原有AlertDialog使用效果对比:


<span style="font-size:18px;">package com.example.iosalertview;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity implements OnClickListener{
	private Button single_uialertview;
	private Button single_alertdialog;
	private Button double_uialertview;
	private Button double_alertdialog;
	private Button three_uialertview;
	private Button three_alertdialog;
	
	private AlertDialog mAlertDialog;
	private CustomDialog mCustomDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		single_uialertview = (Button) findViewById(R.id.single_uialertview);
		single_alertdialog = (Button) findViewById(R.id.single_alertdialog);
		double_uialertview = (Button) findViewById(R.id.double_uialertview);
		double_alertdialog = (Button) findViewById(R.id.double_alertdialog);
		three_uialertview = (Button) findViewById(R.id.three_uialertview);
		three_alertdialog = (Button) findViewById(R.id.three_alertdialog);
		
		single_uialertview.setOnClickListener(this);
		single_alertdialog.setOnClickListener(this);
		double_uialertview.setOnClickListener(this);
		double_alertdialog.setOnClickListener(this);
		three_uialertview.setOnClickListener(this);
		three_alertdialog.setOnClickListener(this);
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.single_uialertview:
			mCustomDialog = new CustomDialog.Builder(MainActivity.this)
			.setTitle(R.string.prompt)
			.setMessage(R.string.exit_app)
			.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Toast.makeText(getApplicationContext(), "点击确定", 0).show();
					mCustomDialog.dismiss();
				}
			})
			.create();
			//设置点击对话框外部区域是否可以取消
			mCustomDialog.setCancelable(true);
			mCustomDialog.show();
			break;
		case R.id.single_alertdialog:
			mAlertDialog = new AlertDialog.Builder(MainActivity.this)
			.setTitle(R.string.prompt)
			.setMessage(R.string.exit_app)
			.setPositiveButton(R.string.confirm, null)
			.create();
			mAlertDialog.show();
			break;
		case R.id.double_uialertview:
			mCustomDialog = new CustomDialog.Builder(MainActivity.this).
			setTitle(R.string.prompt).
			setMessage(R.string.exit_app).
			setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(getApplicationContext(), "点击确定", 0).show();
					mCustomDialog.dismiss();
				}
			}).
			setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(getApplicationContext(), "点击取消", 0).show();
					mCustomDialog.dismiss();
				}
			}).create();
			//设置点击对话框外部区域是否可以取消
			mCustomDialog.setCancelable(false);
			mCustomDialog.show();
			break;
		case R.id.double_alertdialog:
			mAlertDialog = new AlertDialog.Builder(MainActivity.this)
			.setTitle(R.string.prompt)
			.setMessage(R.string.exit_app)
			.setPositiveButton(R.string.confirm, null)
			.setNegativeButton(R.string.cancel, null)
			.create();
			mAlertDialog.show();
			break;
		case R.id.three_uialertview:
			mCustomDialog = new CustomDialog.Builder(MainActivity.this)
			.setTitle(R.string.prompt)
			.setMessage(R.string.exit_app)
			.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Toast.makeText(getApplicationContext(), "点击确定", 0).show();
					mCustomDialog.dismiss();
				}
			})
			.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Toast.makeText(getApplicationContext(), "点击取消", 0).show();
					mCustomDialog.dismiss();
				}
			})
			.setNeutralButton(R.string.dismiss, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Toast.makeText(getApplicationContext(), "点击隐藏", 0).show();
					mCustomDialog.dismiss();
				}
			})
			.create();
			//设置点击对话框外部区域是否可以取消
			mCustomDialog.setCancelable(false);
			mCustomDialog.show();
			break;
		case R.id.three_alertdialog:
			mAlertDialog = new AlertDialog.Builder(MainActivity.this)
			.setTitle(R.string.prompt)
			.setMessage(R.string.exit_app)
			.setPositiveButton(R.string.confirm, null)
			.setNegativeButton(R.string.cancel, null)
			.setNeutralButton(R.string.dismiss, null)
			.create();
			mAlertDialog.show();
			break;


		default:
			break;
		}
	}

}</span>

自定义CustomDialog代码:


package com.example.iosalertview;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomDialog extends Dialog {

	public CustomDialog(Context context) {
		super(context);
	}

	public CustomDialog(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private Context context; // 上下文对象
		private String title; // 对话框标题
		private String message; // 对话框内容
		private String confirm_btnText; // 按钮名称“确定”
		private String cancel_btnText; // 按钮名称“取消”
		private String neutral_btnText; // 按钮名称“隐藏”
		private View contentView; // 对话框中间加载的其他布局界面
		/* 按钮坚挺事件 */
		private DialogInterface.OnClickListener confirm_btnClickListener;
		private DialogInterface.OnClickListener cancel_btnClickListener;
		private DialogInterface.OnClickListener neutral_btnClickListener;

		public Builder(Context context) {
			this.context = context;
		}

		/* 设置对话框信息 */
		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		/**
		 * Set the Dialog message from resource
		 * 
		 * @param title
		 * @return
		 */
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		/**
		 * Set the Dialog title from resource
		 * 
		 * @param title
		 * @return
		 */
		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;
		}

		/**
		 * Set the Dialog title from String
		 * 
		 * @param title
		 * @return
		 */
		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		/**
		 * 设置对话框界面
		 * 
		 * @param v
		 *            View
		 * @return
		 */
		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		/**
		 * Set the positive button resource and it's listener
		 * 
		 * @param confirm_btnText
		 * @return
		 */
		public Builder setPositiveButton(int confirm_btnText,
				DialogInterface.OnClickListener listener) {
			this.confirm_btnText = (String) context.getText(confirm_btnText);
			this.confirm_btnClickListener = listener;
			return this;
		}

		/**
		 * Set the positive button and it's listener
		 * 
		 * @param confirm_btnText
		 * @return
		 */
		public Builder setPositiveButton(String confirm_btnText,
				DialogInterface.OnClickListener listener) {
			this.confirm_btnText = confirm_btnText;
			this.confirm_btnClickListener = listener;
			return this;
		}

		/**
		 * Set the negative button resource and it's listener
		 * 
		 * @param confirm_btnText
		 * @return
		 */
		public Builder setNegativeButton(int cancel_btnText,
				DialogInterface.OnClickListener listener) {
			this.cancel_btnText = (String) context.getText(cancel_btnText);
			this.cancel_btnClickListener = listener;
			return this;
		}

		/**
		 * Set the negative button and it's listener
		 * 
		 * @param confirm_btnText
		 * @return
		 */
		public Builder setNegativeButton(String cancel_btnText,
				DialogInterface.OnClickListener listener) {
			this.cancel_btnText = cancel_btnText;
			this.cancel_btnClickListener = listener;
			return this;
		}

		/**
		 * Set the netural button resource and it's listener
		 * 
		 * @param confirm_btnText
		 * @return
		 */
		public Builder setNeutralButton(int neutral_btnText,
				DialogInterface.OnClickListener listener) {
			this.neutral_btnText = (String) context.getText(neutral_btnText);
			this.neutral_btnClickListener = listener;
			return this;
		}

		/**
		 * Set the netural button and it's listener
		 * 
		 * @param confirm_btnText
		 * @return
		 */
		public Builder setNeutralButton(String neutral_btnText,
				DialogInterface.OnClickListener listener) {
			this.neutral_btnText = neutral_btnText;
			this.neutral_btnClickListener = listener;
			return this;
		}

		public CustomDialog create() {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			// instantiate the dialog with the custom Theme
			final CustomDialog dialog = new CustomDialog(context,
					R.style.mystyle);
			View layout = inflater.inflate(R.layout.customdialog, null);
			dialog.addContentView(layout, new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			// set the dialog title
			((TextView) layout.findViewById(R.id.title)).setText(title);
			((TextView) layout.findViewById(R.id.title)).getPaint()
					.setFakeBoldText(true);

			if (title == null || title.trim().length() == 0) {
				((TextView) layout.findViewById(R.id.message))
						.setGravity(Gravity.CENTER);
			}

			if (neutral_btnText != null && confirm_btnText != null
					&& cancel_btnText != null) {
				((Button) layout.findViewById(R.id.confirm_btn))
						.setText(confirm_btnText);
				if (neutral_btnClickListener != null) {
					((Button) layout.findViewById(R.id.neutral_btn))
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									neutral_btnClickListener.onClick(dialog,
											DialogInterface.BUTTON_NEUTRAL);
								}
							});
				} else {
					((Button) layout.findViewById(R.id.neutral_btn))
							.setOnClickListener(new View.OnClickListener() {

								@Override
								public void onClick(View v) {
									dialog.dismiss();
								}
							});
				}
			} else {
				// if no confirm button or cancle button or neutral just set the
				// visibility to GONE
				layout.findViewById(R.id.neutral_btn).setVisibility(View.GONE);
				layout.findViewById(R.id.single_line).setVisibility(View.GONE);
			}
			// set the confirm button
			if (confirm_btnText != null) {
				((Button) layout.findViewById(R.id.confirm_btn))
						.setText(confirm_btnText);
				if (confirm_btnClickListener != null) {
					((Button) layout.findViewById(R.id.confirm_btn))
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									confirm_btnClickListener.onClick(dialog,
											DialogInterface.BUTTON_POSITIVE);
								}
							});
				} else {
					((Button) layout.findViewById(R.id.confirm_btn))
							.setOnClickListener(new View.OnClickListener() {

								@Override
								public void onClick(View v) {
									dialog.dismiss();
								}
							});
				}
			} else {
				// if no confirm button just set the visibility to GONE
				layout.findViewById(R.id.confirm_btn).setVisibility(View.GONE);
				layout.findViewById(R.id.second_line).setVisibility(View.GONE);
				layout.findViewById(R.id.cancel_btn).setBackgroundResource(
						R.drawable.single_btn_select);
			}
			// set the cancel button
			if (cancel_btnText != null) {
				((Button) layout.findViewById(R.id.cancel_btn))
						.setText(cancel_btnText);
				if (cancel_btnClickListener != null) {
					((Button) layout.findViewById(R.id.cancel_btn))
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									cancel_btnClickListener.onClick(dialog,
											DialogInterface.BUTTON_NEGATIVE);
								}
							});
				} else {
					((Button) layout.findViewById(R.id.cancel_btn))
							.setOnClickListener(new View.OnClickListener() {

								@Override
								public void onClick(View v) {
									dialog.dismiss();
								}
							});
				}
			} else {
				// if no cancel button just set the visibility to GONE
				layout.findViewById(R.id.cancel_btn).setVisibility(View.GONE);
				layout.findViewById(R.id.second_line).setVisibility(View.GONE);
				layout.findViewById(R.id.confirm_btn).setBackgroundResource(
						R.drawable.single_btn_select);
			}
			// set the content message
			if (message != null) {
				((TextView) layout.findViewById(R.id.message)).setText(message);
			} else if (contentView != null) {
				// if no message set
				// add the contentView to the dialog body
				((LinearLayout) layout.findViewById(R.id.message))
						.removeAllViews();
				((LinearLayout) layout.findViewById(R.id.message)).addView(
						contentView, new LayoutParams(
								LayoutParams.WRAP_CONTENT,
								LayoutParams.WRAP_CONTENT));
			}
			dialog.setContentView(layout);
			return dialog;
		}

	}
}


完整Demo下载地址:

http://download.csdn.net/detail/lvshuchangyin/9533120


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值