自定义Dialog

因为最近接手的项目需求,就自己写了一个自定义的Dialog,模仿系统的AlartDialog自定义的一个Dialog

创建MyDialog继承Dialog

创建Buider

创建Dialog

设置标题和内容

设置确定和取消按钮

具体代码如下

package com.example.customdialog;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MyDialog extends Dialog {

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

	public MyDialog(Context context) {
		super(context);
	}
	@SuppressLint("NewApi") public static class Buider{
		private Context context;
		private String title;
		private String message;
		private String confimButtonText;
		private String cancelButtonText;
		private DialogInterface.OnClickListener confimClickListener,cancelButtonClickListener;
		private Button confim;
//		private View TextView mMessage;
		private Button cancel;
		
		public Buider(Context context) {
			this.context = context;
			
		}
		public Buider setMessage(String message){
			this.message = message;
			return this;
		}
		public Buider setMessage(int stringId){
			String message = this.context.getString(stringId);
			setMessage(message);
			return this;
		}
		
		public Buider setTitle(String title){
			this.title = title;
			return this;
		}
		public Buider setTiele(int titleId){
			String title = this.context.getString(titleId);
			this.title = title;
			return this;
		}
		public Buider setContentView(View v){
			return this;
		}
		public Buider setCancelButton(String cancelButtonText,DialogInterface.OnClickListener cancelClickListener){
			this.cancelButtonText = cancelButtonText;
			this.cancelButtonClickListener = cancelClickListener;
			return this;
		}
		public Buider setCancelButton(int cancelButtontextId,DialogInterface.OnClickListener cancelClickListener){
			String text = this.context.getString(cancelButtontextId);
			this.cancelButtonText = text;
			this.cancelButtonClickListener = cancelClickListener;
			return this;
		}
		public Buider setConfimButton(int confimButtontextId,DialogInterface.OnClickListener confimClickListener){
			String text = this.context.getString(confimButtontextId);
			this.confimButtonText = text;
			this.confimClickListener = confimClickListener;
			return this;
		}
		public Buider setConfimButton(String canfimButtonText,DialogInterface.OnClickListener canfimClickListener){
			this.confimButtonText = canfimButtonText;
			this.confimClickListener = canfimClickListener;
			return this;
		}
		@SuppressLint("NewApi") public MyDialog create(){
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final MyDialog dialog = new MyDialog(context,R.style.Dialog);
			View dialog_view = inflater.inflate(R.layout.custom_dialog, null);
			dialog.addContentView(dialog_view,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) );
			TextView mTitle = (TextView) dialog_view.findViewById(R.id.title);
			mTitle.setText(title);
			if(!message.isEmpty()){
				TextView mMessage = (android.widget.TextView) dialog_view.findViewById(R.id.message);
				mMessage.setText(message);
			}
			if(!confimButtonText.isEmpty()){
				confim = (Button) dialog_view.findViewById(R.id.confim);
				confim.setText(confimButtonText);
				confim.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View paramView) {
						confimClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
					}
				});
			}
			if(!(cancelButtonText == null)){
				cancel = (Button) dialog_view.findViewById(R.id.cancel);
				cancel.setText(cancelButtonText);
				cancel.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View paramView) {
						cancelButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
					}
				});
			}
			dialog.setContentView(dialog_view);
			return dialog;
			
		}
	} 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值