自定义Dialog实现(代码是网上找的。。。)





1、dialog的自定义属性

  <!-- dialog -->
    <style name="TextAppearance">
        <item name="android:textColor">#000000</item>
        <item name="android:textColorHighlight">#FFFFFF</item>
        <item name="android:textColorHint">#FFFFFF</item>
        <item name="android:textColorLink">#FFFFFF</item>
        <item name="android:textSize">16sp</item>
        <item name="android:textStyle">normal</item>
    </style>

    <style name="TextAppearance.DialogWindowTitle">
        <item name="android:textSize">18sp</item>
    </style>

    <style name="DialogWindowTitle">
        <item name="android:maxLines">1</item>
        <item name="android:scrollHorizontally">true</item>
        <item name="android:textAppearance">@style/TextAppearance.DialogWindowTitle</item>
    </style>

    <style name="alert">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowTitleStyle">@style/DialogWindowTitle</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
    </style>


2、dialog需要的视图xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/base_dialog_bg"
    android:minWidth="300dp"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/dialog_top"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/dialog_titleicon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:contentDescription="头像"
            android:paddingLeft="5dp" />

        <TextView
            android:id="@+id/dialog_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="提示"
            android:textColor="#f82c22" />
    </LinearLayout>

    <View
        android:id="@+id/title_red_line"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="@drawable/base_horizontal_line_red" />

    <LinearLayout
        android:id="@+id/dialog_contentPanel"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingBottom="15dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="15dp" >

        <TextView
            android:id="@+id/dialog_message"
            android:layout_width="match_parent"
            android:gravity="left|center"
            android:layout_height="wrap_content"
            android:lineSpacingMultiplier="1.2"
            android:textColor="#3e3e39" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/dialog_customPanel"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1" >

        <FrameLayout
            android:id="@+id/dialog_custom"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:layout_height="wrap_content" />
    </FrameLayout>

    <View
        android:id="@+id/view"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#cacaca" />

    <LinearLayout
        android:id="@+id/dialog_bottom"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/dialog_leftspacer"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:orientation="horizontal"
            android:visibility="gone" />

        <Button
            android:id="@+id/dialog_negativebutton"
            android:layout_width="0px"
            android:layout_height="35dp"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:background="@drawable/dialog_btn_selector"
            android:maxLines="2"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:singleLine="true"
            android:textColor="#3e3e39" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#cacaca" />

        <Button
            android:id="@+id/dialog_positivebutton"
            android:layout_width="0px"
            android:layout_height="35dp"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:background="@drawable/dialog_btn_selector"
            android:gravity="center"
            android:maxLines="2"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:singleLine="true"
            android:textColor="#3e3e39" />

        <LinearLayout
            android:id="@+id/dialog_rightspacer"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:orientation="horizontal"
            android:visibility="gone" />
    </LinearLayout>

</LinearLayout>


3、dialog基类

package com.example.gaoxinzhilu.view;


import com.example.gaoxinzhilu.R;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 *自定义对话框基类
 *支持:对话框全屏显示控制、title显示控制,一个button或两个
 */
public abstract class DialogBase extends Dialog {
	protected OnClickListener onSuccessListener;
	protected Context mainContext;
	protected OnClickListener onCancelListener;//提供给取消按钮
	protected OnDismissListener onDismissListener;
	
	protected View view;
	protected Button positiveButton, negativeButton;
	private boolean isFullScreen = false;
	
	private boolean hasTitle = true;//是否有title
	
	private int width = 0, height = 0, x = 0, y = 0;
	private int iconTitle = 0;
	private String message, title;
	private String namePositiveButton, nameNegativeButton;
	private final int MATCH_PARENT = android.view.ViewGroup.LayoutParams.MATCH_PARENT;
	private final int WRAP_CONTENT = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
	private Bitmap bitmap;
	private boolean isCancel = true;//默认是否可点击back按键/点击外部区域取消对话框
	
	public void setBitmap(Bitmap map) {
		this.bitmap = map;
	}
	public Bitmap getBitmap() {
		return bitmap ;
	}
	public boolean isCancel() {
		return isCancel;
	}

	public void setCancel(boolean isCancel) {
		this.isCancel = isCancel;
	}

	/**
	 * 构造函数
	 * @param context 对象应该是Activity
	 */
	public DialogBase(Context context) {
		super(context, R.style.alert);
		this.mainContext = context;
	}
	
	/** 
	 * 创建事件
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
    	setContentView(R.layout.v2_dialog_base);
		this.onBuilding();
		// 设置标题和消息
		LinearLayout dialog_top = (LinearLayout)findViewById(R.id.dialog_top);
		View title_red_line = (View)findViewById(R.id.title_red_line);
		
		//是否有title
		if(hasTitle){
			dialog_top.setVisibility(View.VISIBLE);
			title_red_line.setVisibility(View.VISIBLE);
		}else{
			dialog_top.setVisibility(View.GONE);
			title_red_line.setVisibility(View.GONE);
		}
		TextView titleTextView = (TextView)findViewById(R.id.dialog_title);
		titleTextView.setText(this.getTitle());
		TextView messageTextView = (TextView)findViewById(R.id.dialog_message);
		messageTextView.setText(this.getMessage());
		
		if (view != null) {
			FrameLayout custom = (FrameLayout) findViewById(R.id.dialog_custom);
			int w=getBitmap().getWidth();
			int h=getBitmap().getHeight();
			custom.addView(view, new LayoutParams(w,h ));
			android.widget.FrameLayout.LayoutParams params=(android.widget.FrameLayout.LayoutParams) custom.getLayoutParams();
			params.width=w;
			params.height=h;
			custom.setLayoutParams(params);
			
			findViewById(R.id.dialog_contentPanel).setVisibility(View.GONE);
		} else {
			findViewById(R.id.dialog_customPanel).setVisibility(View.GONE);
		}

		// 设置按钮事件监听
		positiveButton = (Button)findViewById(R.id.dialog_positivebutton);
		negativeButton = (Button)findViewById(R.id.dialog_negativebutton);
		if(namePositiveButton != null && namePositiveButton.length()>0){
			positiveButton.setText(namePositiveButton);
			positiveButton.setOnClickListener(GetPositiveButtonOnClickListener());
		} else {
			positiveButton.setVisibility(View.GONE);
//			findViewById(R.id.dialog_leftspacer).setVisibility(View.VISIBLE);
//			findViewById(R.id.dialog_rightspacer).setVisibility(View.VISIBLE);
		}
		if(nameNegativeButton != null && nameNegativeButton.length()>0){
			negativeButton.setText(nameNegativeButton);
			negativeButton.setOnClickListener(GetNegativeButtonOnClickListener());
		} else {
			negativeButton.setVisibility(View.GONE);
		}
		if(!(namePositiveButton != null && namePositiveButton.length()>0)&&!(nameNegativeButton != null && nameNegativeButton.length()>0)){
			findViewById(R.id.view).setVisibility(View.GONE);
			findViewById(R.id.dialog_bottom).setVisibility(View.GONE);
		}//当两个按钮都没有文字的时候,隐藏下方的按钮区域及分割线
		// 设置对话框的位置和大小
		LayoutParams params = this.getWindow().getAttributes();  
		if(this.getWidth()>0)
			params.width = this.getWidth();  
		if(this.getHeight()>0)
			params.height = this.getHeight();  
		if(this.getX()>0)
			params.width = this.getX();  
		if(this.getY()>0)
			params.height = this.getY();  
		
		// 如果设置为全屏
		if(isFullScreen) {
			params.width = WindowManager.LayoutParams.MATCH_PARENT;
			params.height = WindowManager.LayoutParams.MATCH_PARENT;
		}
		
		//设置点击dialog外部区域可取消
		if(isCancel){
			setCanceledOnTouchOutside(true);
			setCancelable(true);
		}else{
			setCanceledOnTouchOutside(false);
			setCancelable(false);
		}
	    getWindow().setAttributes(params);  
		this.setOnDismissListener(GetOnDismissListener());
		this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
	}

	/**
	 * 获取OnDismiss事件监听,释放资源
	 * @return OnDismiss事件监听
	 */
	protected OnDismissListener GetOnDismissListener() {
		return new OnDismissListener(){
			public void onDismiss(DialogInterface arg0) {
				DialogBase.this.onDismiss();
				DialogBase.this.setOnDismissListener(null);
				view = null;
				mainContext = null;
				positiveButton = null;
				negativeButton = null;
				if(onDismissListener != null){
					onDismissListener.onDismiss(null);
				}
			}			
		};
	}

	/**
	 * 获取确认按钮单击事件监听
	 * @return 确认按钮单击事件监听
	 */
	protected View.OnClickListener GetPositiveButtonOnClickListener() {
		return new View.OnClickListener() {
			public void onClick(View v) {
				if(OnClickPositiveButton())
					DialogBase.this.dismiss();
			}
		};
	}
	
	/**
	 * 获取取消按钮单击事件监听
	 * @return 取消按钮单击事件监听
	 */
	protected View.OnClickListener GetNegativeButtonOnClickListener() {
		return new View.OnClickListener() {
			public void onClick(View v) {
				OnClickNegativeButton();
				DialogBase.this.dismiss();
			}
		};
	}
	
	/**
	 * 获取焦点改变事件监听,设置EditText文本默认全选
	 * @return 焦点改变事件监听
	 */
	protected OnFocusChangeListener GetOnFocusChangeListener() {
		return new OnFocusChangeListener() {
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus && v instanceof EditText) {
					((EditText) v).setSelection(0, ((EditText) v).getText().length());
				}
			}
		};
	}
	
	/**
	 * 设置成功事件监听,用于提供给调用者的回调函数
	 * @param listener 成功事件监听
	 */
	public void SetOnSuccessListener(OnClickListener listener){
		onSuccessListener = listener;
	}
	
	/**
	 * 设置关闭事件监听,用于提供给调用者的回调函数
	 * @param listener 关闭事件监听
	 */
	public void SetOnDismissListener(OnDismissListener listener){
		onDismissListener = listener;
	}

	/**提供给取消按钮,用于实现类定制
	 * @param listener
	 */
	public void SetOnCancelListener(OnClickListener listener){
		onCancelListener = listener;
	}
	
	/**
	 * 创建方法,用于子类定制创建过程
	 */
	protected abstract void onBuilding();

	/**
	 * 确认按钮单击方法,用于子类定制
	 */
	protected abstract boolean OnClickPositiveButton();

	/**
	 * 取消按钮单击方法,用于子类定制
	 */
	protected abstract void OnClickNegativeButton();

	/**
	 * 关闭方法,用于子类定制
	 */
	protected abstract void onDismiss();

	/**
	 * @return 对话框标题
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title 对话框标题
	 */
	public void setTitle(String title) {
		this.title = title;
	}
	
	/**
	 * @param iconTitle 标题图标的资源Id
	 */
	public void setIconTitle(int iconTitle) {
		this.iconTitle = iconTitle;
	}

	/**
	 * @return 标题图标的资源Id
	 */
	public int getIconTitle() {
		return iconTitle;
	}

	/**
	 * @return 对话框提示信息
	 */
	protected String getMessage() {
		return message;
	}

	/**
	 * @param message 对话框提示信息
	 */
	protected void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return 对话框View
	 */
	protected View getView() {
		return view;
	}

	/**
	 * @param view 对话框View
	 */
	protected void setView(View view) {
		this.view = view;
	}

	/**
	 * @return 是否全屏
	 */
	public boolean getIsFullScreen() {
		return isFullScreen;
	}

	/**
	 * @param isFullScreen 是否全屏
	 */
	public void setIsFullScreen(boolean isFullScreen) {
		this.isFullScreen = isFullScreen;
	}

	public boolean isHasTitle() {
		return hasTitle;
	}


	public void setHasTitle(boolean hasTitle) {
		this.hasTitle = hasTitle;
	}

	
	/**
	 * @return 对话框宽度
	 */
	protected int getWidth() {
		return width;
	}

	/**
	 * @param width 对话框宽度
	 */
	protected void setWidth(int width) {
		this.width = width;
	}

	/**
	 * @return 对话框高度
	 */
	protected int getHeight() {
		return height;
	}

	/**
	 * @param height 对话框高度
	 */
	protected void setHeight(int height) {
		this.height = height;
	}

	/**
	 * @return 对话框X坐标
	 */
	public int getX() {
		return x;
	}

	/**
	 * @param x 对话框X坐标
	 */
	public void setX(int x) {
		this.x = x;
	}

	/**
	 * @return 对话框Y坐标
	 */
	public int getY() {
		return y;
	}

	/**
	 * @param y 对话框Y坐标
	 */
	public void setY(int y) {
		this.y = y;
	}

	/**
	 * @return 确认按钮名称
	 */
	protected String getNamePositiveButton() {
		return namePositiveButton;
	}

	/**
	 * @param namePositiveButton 确认按钮名称
	 */
	protected void setNamePositiveButton(String namePositiveButton) {
		this.namePositiveButton = namePositiveButton;
	}

	/**
	 * @return 取消按钮名称
	 */
	protected String getNameNegativeButton() {
		return nameNegativeButton;
	}

	/**
	 * @param nameNegativeButton 取消按钮名称
	 */
	protected void setNameNegativeButton(String nameNegativeButton) {
		this.nameNegativeButton = nameNegativeButton;
	}
	
}


4、dialog实现类

package com.example.gaoxinzhilu.view;


import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;


/**
 * 提示对话框,有一个确认、一个返回按钮
 */
public class DialogTips extends DialogBase {
	boolean hasNegative;
	boolean hasTitle;
	/**
	 * 构造函数
	 * @param context
	 */
	public DialogTips(Context context, String title,String message,String buttonText,boolean hasNegative,boolean hasTitle) {
		super(context);
		super.setMessage(message);
		super.setNamePositiveButton(buttonText);
		this.hasNegative = hasNegative;
		this.hasTitle = hasTitle;
		super.setTitle(title);
	}
	/**
	 * 只有view
	 */
	public DialogTips(Context context,View v,Bitmap b){
		super(context);
		super.setBitmap(b);
		super.setView(v);//设置View
		this.hasNegative = false;
		this.hasTitle = false;//没有标题栏
		super.setCancel(true);//默认是否可点击back按键/点击外部区域取消对话框
	}
	/**下线通知的对话框样式
	 * @param context
	 * @param title
	 * @param message
	 * @param buttonText
	 */
	public DialogTips(Context context,String message,String buttonText) {
		super(context);
		super.setMessage(message);
		super.setNamePositiveButton(buttonText);
		this.hasNegative = false;
		this.hasTitle = true;
		super.setTitle("提示");
		super.setCancel(false);
	}
	
	public DialogTips(Context context, String message,String buttonText,String negetiveText,String title,boolean isCancel) {
		super(context);
		super.setMessage(message);
		super.setNamePositiveButton(buttonText);
		this.hasNegative=false;
		super.setNameNegativeButton(negetiveText);
		this.hasTitle = true;
		super.setTitle(title);
		super.setCancel(isCancel);
	}


	/**
	 * 创建对话框
	 */
	@Override
	protected void onBuilding() {
		super.setWidth(dip2px(mainContext, 300));
		if(hasNegative){
			super.setNameNegativeButton("取消");
		}
		if(!hasTitle){
			super.setHasTitle(false);
		}
	}


	public int dip2px(Context context,float dipValue){
		float scale=context.getResources().getDisplayMetrics().density;		
		return (int) (scale*dipValue+0.5f);		
	}
	
	@Override
	protected void onDismiss() { }


	@Override
	protected void OnClickNegativeButton() { 
		if(onCancelListener != null){
			onCancelListener.onClick(this, 0);
		}
	}


	/**
	 * 确认按钮,触发onSuccessListener的onClick
	 */
	@Override
	protected boolean OnClickPositiveButton() { 
		if(onSuccessListener != null){
			onSuccessListener.onClick(this, 1);
		}
		return true;
	}
}


5、dialog使用类

package com.example.gaoxinzhilu.view;


import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout.LayoutParams;


public class DialogActivity extends Activity {
	DialogTips log;
Bitmap[] bitmaps;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		bitmaps=new Bitmap[72];
		for (int i = 0; i < 72; i++) {
			bitmaps[i] = Utils.createBitmapFromAssets("/assets/image/i0011_" + (1 + i) + ".JPG");
		}
		View view=new CustomViews(this, bitmaps);
		int w=bitmaps[0].getWidth();
		int h=bitmaps[0].getHeight();
		log = new DialogTips(this,view,bitmaps[0]);
		log.show();
	}


	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		log.dismiss();
	}
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值