自定义Dialog 通过回调函数CallBack接口实现自定义风格

package com.vtion.sleb.view;


import com.vtion.sleb.activities.R;

import android.app.Dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.TextView;


/**
 *自定义dialog 用来检测用户是否联网 ,如果没有点击确定,自动跳转到网络设置页面
 * 
 * @author lichengyuan
 * @date 2012.11.20
 * */
public class VDialog {
	private Dialog dialog;
	private Context context;
	private VDialogClickListener onClickListener;
	private VDialogNeutralListener onNeutralListener;
	
	private boolean hasok = false;
	private boolean hasneutral = false;
	private boolean hascancel = false;
	private String msg = "信息提示...";

	private String okString = "确定";
	private String neutralString = "";
	private String cancelString = "取消";

	
	public VDialog(Context context, String msg) {

		// super(context, R.style.mDialog);
		dialog = new Dialog(context, R.style.mDialog);
		this.context = context;
		this.msg = msg;
		setCancel(true);
	}

	
	public VDialog(Context context, VDialogClickListener clickListener,
			VDialogNeutralListener neutralListener, String msg) {

		// super(context, R.style.mDialog);
		dialog = new Dialog(context, R.style.mDialog);

		this.onClickListener = clickListener;
		this.onNeutralListener = neutralListener;
		this.context = context;
		this.msg = msg;
		hasok = true;
		hascancel = true;
		hasneutral = true;
//		onCreate(null);
	}

	
	public VDialog(Context context, VDialogClickListener vdlistener, String msg) {

		dialog = new Dialog(context, R.style.mDialog);

		this.onClickListener = vdlistener;
		this.context = context;
		this.msg = msg;
		hasok = true;
		hascancel = true;
		hasneutral = false;
	}

	
	public VDialog(Context context, String msg,boolean hasOk,boolean hasCancel, boolean hasNeutral) {

		dialog = new Dialog(context, R.style.mDialog);
		this.context = context;
		this.msg = msg;
		this.hasok = hasOk;
		this.hasneutral = hasNeutral;
		this.hascancel = hasCancel;
		onCreate(null);
	}
	
	public void setOkStr(String str) {
		this.okString = str;
	}

	public void setCancelStr(String str) {
		this.cancelString = str;
	}

	public void setNeutralStr(String str) {
		this.neutralString = str;
	}

    private void setMsg(String msg) {
		this.msg = msg;
	}

	protected void onCreate(Bundle savedInstanceState) {
		Window win = dialog.getWindow();

		View view = View.inflate(context, R.layout.mydialog_layout, null);

		LayoutParams lp = dialog.getWindow().getAttributes();

		dialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
			}
		});
		dialog.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				dialog.dismiss();
			}
		});
		dialog.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				
				if(keyCode == KeyEvent.KEYCODE_BACK){
					return true;
				}
				return false;
			}
		});

		TextView tv = (TextView) view.findViewById(R.id.tips);
		tv.setText(msg);

		Button btn_ok = (Button) view.findViewById(R.id.btn_ok);
		Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
		btn_ok.setText(okString);
		btn_cancel.setText(cancelString);
		
		if (hasneutral) {
		} else {
		}
		if (hasok) {
			btn_ok.setVisibility(View.VISIBLE);
		} else {
			btn_ok.setVisibility(View.GONE);
		}
		if (hascancel) {
			btn_cancel.setVisibility(View.VISIBLE);
		} else {
			btn_cancel.setVisibility(View.GONE);
		}
		btn_ok.setOnClickListener(new android.view.View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				onClickListener.onConfirmListener();
//				 context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
			}
		});


		btn_cancel.setOnClickListener(new android.view.View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				if (onClickListener != null)
					onClickListener.onCancelListener();
			}
		});

		dialog.setContentView(view);
	}

	public VDialog setOK(boolean b) {
		this.hasok = b;
		this.hascancel = !b;
		return this;
	}

	public VDialog setNeutral(boolean b) {
		this.hasneutral = b;
		if (b) {
			this.hasok = true;
			this.hascancel = true;
		}else{
			
		}
		return this;
	}

	public VDialog setCancel(boolean b) {
		this.hascancel = b;
		this.hasok = !b;
		this.hasneutral = false;
		return this;
	}

	public void show() {
		onCreate(null);
		dialog.show();
	}

	public void setVDialogOnClickListener(VDialogClickListener vdlistener) {
		this.onClickListener = vdlistener;
	}
	public void setVDialogNeutralListener(VDialogNeutralListener onNeutralListener){
		this.onNeutralListener =  onNeutralListener;
	}

	protected void onStop() {

	}

	public void setMessage(String msg) {
		this.msg = msg;
	}
	
	public boolean isShowing(){
		return this.dialog.isShowing();
	}

	public interface VDialogClickListener {
		public void onConfirmListener();
		public void onCancelListener();
	}

	public interface VDialogNeutralListener {

		
		public void onNeutralListener();

	}
	
}


你可能感兴趣的:(自定义Dialog 通过回调函数CallBack接口实现自定义风格)