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

本文介绍如何通过实现回调函数CallBack接口来自定义Dialog的样式,详细步骤包括定义接口、实现接口方法以及在Dialog中调用接口进行交互,从而达到个性化定制Dialog的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();

	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值