源码下载地址:http://download.csdn.net/detail/haiyangzhisheng/9504180
话题来源:为新的项目添加原来的项目做的提示框时,要把原来项目的自定义Dialog复制过来,整理了一下,需要提示框时只需要复制Java类和一个布局文件和动画文件就行了
使用方式:
final AAMyAlertDialogmDialog = new AAMyAlertDialog(this);
mDialog.setTitle("我的提示");
mDialog.setMessage("提示内容");
mDialog.setPositiveButton("确定",new OnClickListener() {
@Override
public void onClick(Viewv) {
mDialog.dismiss();
}
});
mDialog.setNegativeButton("取消",new OnClickListener() {
@Override
public void onClick(Viewv) {
}
});
mDialog.setMidleButton("操作",new OnClickListener() {
@Override
public void onClick(Viewv) {
}
});
mDialog.show();
实现原理:
1、继承Dialog类
2、GradientDrawable实现圆角背景
3、StateListDrawable实现点击效果
4、在onAttachedToWindow方法中设置弹出框在手机中占比
5、Xml中定义AlphaAnimation、ScaleAnimation等动画类实现动画效果
实现代码解析:
AAMyAlertDialog.java文件
public class AAMyAlertDialogextends Dialog {
/**
* 提示框的三个按钮,自动显示和隐藏
*/
private Button btn_ok, btn_cancle, btn_midle;
/**
* 提示内容
*/
private TextView tv_title, tv_alert, tv_alertred;
/**
* 标题分割线
*/
private ImageView iv_title_line;
/**
* 提示内容变量初始化
*/
private String title, message = "", messagered;
/**
* 按钮显示文本
*/
private String poText, neText, midText;
/**
* 分割线
*/
private ImageView iv_midle, iv_cancle;
/**
* 按钮监听事件
*/
private View.OnClickListenerpoLis, neLis,midLis;
/**
* 标题容器
*/
private LinearLayoutlin_title;
/**
* 弹出框容器
*/
private LinearLayoutline_parent;
/**
* 调用的Activity实例
*/
private Activity myActivity;
/**
* 弹出框背景颜色,默认白色
*/
private int dialogBgColor = Color.WHITE;
/**
* 弹出框圆角半径,默认10,单位dip
*/
private int dialogRadius = 10;
/**
* 按钮默认颜色,默认白色
*/
private int btnDefaultColor = Color.WHITE;
/**
* 按钮点击颜色,默认灰色
*/
private int btnPressedColor = Color.GRAY;
/**
* 标题字体颜色
*/
private int titleColor = Color.BLUE;
/**
* 标题分割线颜色
*/
private int titleLineColor = Color.BLUE;
/**
* 构造方法,默认设置点击外部不可消失
*
* @param activity
*/
public AAMyAlertDialog(Activityactivity) {
super(activity);
this.myActivity =activity;
this.setCanceledOnTouchOutside(false);
}
/**
* 设置标题
*
* @param title
*/
public void setTitle(Stringtitle) {
this.title =title;
}
/**
* 设置显示内容
*
* @param message
*/
public void setMessage(Stringmessage) {
this.message =message;
}
/**
* 设置红色提醒内容
*
* @param message
*/
public void setMessageRed(Stringmessage) {
this.messagered =message;
}
/**
* 设置确定按钮内容和监听,当只有一个按钮时,只设置这个方法,否则布局会异常
*
* @param text
* @param l
*/
public void setPositiveButton(Stringtext, View.OnClickListener l) {
this.poText =text;
this.poLis =l;
}
/**
* 设置取消按钮内容和监听
*
* @param text
* @param l
*/
public void setNegativeButton(Stringtext, View.OnClickListener l) {
this.neText =text;
this.neLis =l;
}
/**
* 设置中间按钮内容和监听
*
* @param text
* @param l
*/
public void setMidleButton(Stringtext, View.OnClickListener l) {
this.midText =text;
this.midLis =l;
}
/**
* 设置弹出框背景颜色
*/
public void setDialogBgColor(int color) {
this.dialogBgColor =color;
}
/**
* 设置弹出框圆角半径
*
* @param radius
*/
public void setDialogRadius(int radius) {
this.dialogRadius =radius;
}
@Override
public void show() {
super.show();
Window window = getWindow(); // 得到对话框
window.setWindowAnimations(R.style.dialogWindowAnim);// 设置窗口弹出动画
// 设置对话框背景为透明
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window dialogWindow = getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
int width =wm.getDefaultDisplay().getWidth();
// 获取屏幕宽、高用
// p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6
lp.width = (int) (width * 0.65);// 宽度设置为屏幕的0.95
dialogWindow.setAttributes(lp);
}
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 初始化变量,将半径的dp转换为pix
dialogRadius = dip2px(myActivity,dialogRadius);
setContentView(R.layout.aa_my_alert_dialog);
btn_ok = (Button) findViewById(R.id.btn_ok);
btn_cancle = (Button) findViewById(R.id.btn_cancle);
btn_midle = (Button) findViewById(R.id.btn_midle);
tv_title = (TextView) findViewById(R.id.tv_title);
tv_alert = (TextView) findViewById(R.id.tv_alert);
tv_alertred = (TextView) findViewById(R.id.tv_alertred);
iv_midle = (ImageView) findViewById(R.id.iv_midle);
iv_cancle = (ImageView) findViewById(R.id.iv_cancle);
lin_title = (LinearLayout) findViewById(R.id.lin_title);
line_parent = (LinearLayout) findViewById(R.id.line_parent);
iv_title_line = (ImageView) findViewById(R.id.iv_title_line);
// 初始化布局颜色
tv_title.setTextColor(titleColor);
iv_title_line.setBackgroundColor(titleLineColor);
// 设置弹出框背景
GradientDrawable mbg = new GradientDrawable();// 创建drawable
mbg.setColor(dialogBgColor);
mbg.setCornerRadius(dialogRadius);
line_parent.setBackgroundDrawable(mbg);
if (message.contains("")) {
tv_alert.setText(Html.fromHtml(message));
} else {
tv_alert.setText(message);
}
// 未设置btn_ok隐藏
btn_ok.setText(poText);
btn_ok.setOnClickListener(poLis);
// 通过按钮数量设置按钮样式
int buttonCount = 1;
// 未设置btn_cancle隐藏
if (neText ==null) {
btn_cancle.setVisibility(8);
iv_cancle.setVisibility(8);
} else {
buttonCount++;
btn_cancle.setVisibility(0);
btn_cancle.setText(neText);
btn_cancle.setOnClickListener(neLis);
iv_cancle.setVisibility(0);
}
if (midText ==null) {
btn_midle.setVisibility(8);
iv_midle.setVisibility(8);
} else {
buttonCount++;
btn_midle.setVisibility(0);
iv_midle.setVisibility(0);
btn_midle.setText(midText);
btn_midle.setOnClickListener(midLis);
}
if (title ==null) {
lin_title.setVisibility(8);
} else {
lin_title.setVisibility(0);
tv_title.setText(title);
}
if (messagered ==null) {
tv_alertred.setVisibility(8);
} else {
tv_alertred.setText(messagered);
}
// ------------------设置按钮点击效果
if (buttonCount == 1) {
// 默认背景
GradientDrawable gd1 = new GradientDrawable();// 创建drawable
gd1.setColor(btnDefaultColor);
gd1.setCornerRadii(new float[] { 0, 0, 0, 0,dialogRadius, dialogRadius, dialogRadius, dialogRadius });
// 点击背景
GradientDrawable gd2 = new GradientDrawable();// 创建drawable
gd2.setColor(btnPressedColor);
gd2.setCornerRadii(new float[] { 0, 0, 0, 0,dialogRadius, dialogRadius, dialogRadius, dialogRadius });
// 定义选择器
StateListDrawable selector = new StateListDrawable();
selector.addState(new int[] { -android.R.attr.state_pressed },gd1);
selector.addState(new int[] { android.R.attr.state_pressed },gd2);
btn_ok.setBackgroundDrawable(selector);
}
if (buttonCount == 2) {
try {
// 默认背景
GradientDrawable gd1 = new GradientDrawable();// 创建drawable
gd1.setColor(btnDefaultColor);
gd1.setCornerRadii(new float[] { 0, 0, 0, 0,dialogRadius, dialogRadius, 0, 0 });
// 点击背景
GradientDrawable gd2 = new GradientDrawable();// 创建drawable
gd2.setColor(btnPressedColor);
gd2.setCornerRadii(new float[] { 0, 0, 0, 0,dialogRadius, dialogRadius, 0, 0 });
// 定义选择器
StateListDrawable selector = new StateListDrawable();
selector.addState(new int[] { -android.R.attr.state_pressed },gd1);
selector.addState(new int[] { android.R.attr.state_pressed },gd2);
btn_cancle.setBackgroundDrawable(selector);
} catch (Exception e) {
e.printStackTrace();
}
try {
// 默认背景
GradientDrawable gd1 = new GradientDrawable();// 创建drawable
gd1.setColor(btnDefaultColor);
gd1.setCornerRadii(new float[] { 0, 0, 0, 0, 0, 0,dialogRadius, dialogRadius });
// 点击背景
GradientDrawable gd2 = new GradientDrawable();// 创建drawable
gd2.setColor(btnPressedColor);
gd2.setCornerRadii(new float[] { 0, 0, 0, 0, 0, 0,dialogRadius, dialogRadius });
// 定义选择器
StateListDrawable selector = new StateListDrawable();
selector.addState(new int[] { -android.R.attr.state_pressed },gd1);
selector.addState(new int[] { android.R.attr.state_pressed },gd2);
btn_ok.setBackgroundDrawable(selector);
} catch (Exception e) {
e.printStackTrace();
}
}
if (buttonCount == 3) {
try {
// 默认背景
GradientDrawable gd1 = new GradientDrawable();// 创建drawable
gd1.setColor(btnDefaultColor);
gd1.setCornerRadii(new float[] { 0, 0, 0, 0,dialogRadius, dialogRadius, 0, 0 });
// 点击背景
GradientDrawable gd2 = new GradientDrawable();// 创建drawable
gd2.setColor(btnPressedColor);
gd2.setCornerRadii(new float[] { 0, 0, 0, 0,dialogRadius, dialogRadius, 0, 0 });
// 定义选择器
StateListDrawable selector = new StateListDrawable();
selector.addState(new int[] { -android.R.attr.state_pressed },gd1);
selector.addState(new int[] { android.R.attr.state_pressed },gd2);
btn_cancle.setBackgroundDrawable(selector);
} catch (Exception e) {
e.printStackTrace();
}
try {
// 默认背景
GradientDrawable gd1 = new GradientDrawable();// 创建drawable
gd1.setColor(btnDefaultColor);
gd1.setCornerRadii(new float[] { 0, 0, 0, 0, 0, 0,dialogRadius, dialogRadius });
// 点击背景
GradientDrawable gd2 = new GradientDrawable();// 创建drawable
gd2.setColor(btnPressedColor);
gd2.setCornerRadii(new float[] { 0, 0, 0, 0, 0, 0,dialogRadius, dialogRadius });
// 定义选择器
StateListDrawable selector = new StateListDrawable();
selector.addState(new int[] { -android.R.attr.state_pressed },gd1);
selector.addState(new int[] { android.R.attr.state_pressed },gd2);
btn_ok.setBackgroundDrawable(selector);
} catch (Exception e) {
e.printStackTrace();
}
try {
// 默认背景
GradientDrawable gd1 = new GradientDrawable();// 创建drawable
gd1.setColor(btnDefaultColor);
// 点击背景
GradientDrawable gd2 = new GradientDrawable();// 创建drawable
gd2.setColor(btnPressedColor);
// 定义选择器
StateListDrawable selector = new StateListDrawable();
selector.addState(new int[] { -android.R.attr.state_pressed },gd1);
selector.addState(new int[] { android.R.attr.state_pressed },gd2);
btn_midle.setBackgroundDrawable(selector);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 根据手机的分辨率从 dp 的单位 转成为px(像素)
*/
private int dip2px(Contextcontext, float dpValue) {
final float scale =context.getResources().getDisplayMetrics().density;
return (int) (dpValue *scale + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为dp
*/
private int px2dip(Contextcontext, float pxValue) {
final float scale =context.getResources().getDisplayMetrics().density;
return (int) (pxValue /scale + 0.5f);
}
}
aa_common_animations.xml文件
xml version="1.0" encoding="utf-8"?>
<resources>
<style name="dialogWindowAnim" mce_bogus="1" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/aa_dailog_enter_animationitem>
<item name="android:windowExitAnimation">@anim/aa_dailog_exit_animationitem>
style>
<color name="black">#323232color>
resources>
aa_my_alert_dialog.xml
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/line_parent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/lin_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="标题"
android:textSize="18dp" />
<ImageView
android:id="@+id/iv_title_line"
android:layout_width="fill_parent"
android:layout_height="1dp" />
LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:id="@+id/tv_alert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容"
android:textColor="@color/black"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_alertred"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="#ffff4444"
android:textSize="18sp"
android:textStyle="bold" />
LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="5dip"
android:background="#999999" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_ok"
android:layout_width="0dp"
android:layout_height="38dp"
android:layout_weight="1"
android:gravity="center"
android:text="确 定 "
android:textColor="@color/black"
android:textSize="16sp" />
<ImageView
android:id="@+id/iv_midle"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="#999999" />
<Button
android:id="@+id/btn_midle"
android:layout_width="0dp"
android:layout_height="38dp"
android:layout_weight="1"
android:gravity="center"
android:text="返回主菜单"
android:textColor="@color/black"
android:textSize="16sp" />
<ImageView
android:id="@+id/iv_cancle"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="#999999" />
<Button
android:id="@+id/btn_cancle"
android:layout_width="0dp"
android:layout_height="38dp"
android:layout_weight="1"
android:gravity="center"
android:text="取 消"
android:textColor="@color/black"
android:textSize="16sp" />
LinearLayout>
LinearLayout>
aa_dailog_enter_animation.xml文件
xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<scale
android:duration="200"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1"
android:toYScale="1"/>
<alpha
android:duration="200"
android:fromAlpha="0"
android:toAlpha="1"/>
set>
aa_dailog_exit_animation.xml文件
xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<scale
android:duration="200"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0"
android:toYScale="0"/>
<alpha
android:duration="200"
android:fromAlpha="1"
android:toAlpha="0"/>
set>
源码下载地址:http://download.csdn.net/detail/haiyangzhisheng/9504180