自定义Dialog的实现
版权声明:本文为博主原创文章,未经博主允许不得转载。
首先呢这个需求很普遍,在开发中几乎每个项目都会用到,也比较简单,嘎嘎,博主的 处女作哦,较菜谅解。:
让我们来想想一个Dialog它需要写什么呢?
1. 内容几乎是必不可少的了
2.我们也有能用到标题,标题也有可能附带图标
3.也有可能我们需求比较不普通的内容,用不到我们预先的内容显示的控件,直接addView形式展示内容
4.最后一步当然是我们两个按钮的点击事件的回调了
好了,bb了这么就 ,下面直接开车了
首先上一张效果图,这是我看ios效果仿了一下
接下来就是代码实现了
public class BaseDialog extends Dialog {
public BaseDialog(Context context) {
super(context);
}
public BaseDialog(Context context, int themeResId) {
super(context, themeResId);
}
protected BaseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
/**
* Helper class for creating a custom dialog
*/
public static class Builder {
private Context context;
private String title;
private String message;
private String LeftButtonText;
private String RightButtonText;
private View contentView;
private int Image = 0;
private DialogInterface.OnClickListener
positiveButtonClickListener,
negativeButtonClickListener;
public Builder(Context context) {
this.context = context;
}
/**
* Set the Dialog message from String
*
* @param message
* @return
*/
public Builder setMessage(String message) {
this.message = message;
return this;
}
/**
* Set the Dialog message from resource
*
* @param message
* @return
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}
/**
* Set the Dialog title from resource
*
* @param title
* @return
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}
/**
* Set the Dialog title from String
*
* @param title
* @return
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
/**
* Set a custom content view for the Dialog.
* If a message is set, the contentView is not
* added to the Dialog...
*
* @param v
* @return
*/
public Builder setContentView(View v) {
this.contentView = v;
return this;
}
public Builder setImage(int Image) {
this.Image = Image;
return this;
}
/**
* Set the positive button resource and it's listener
*
* @param LeftButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(int LeftButtonText,
DialogInterface.OnClickListener listener) {
this.LeftButtonText = (String) context
.getText(LeftButtonText);
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the positive button text and it's listener
*
* @param LeftButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(String LeftButtonText,
DialogInterface.OnClickListener listener) {
this.LeftButtonText = LeftButtonText;
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the negative button resource and it's listener
*
* @param RightButtonText
* @param listener
* @return
*/
public Builder setNegativeButton(int RightButtonText,
DialogInterface.OnClickListener listener) {
this.RightButtonText = (String) context
.getText(RightButtonText);
this.negativeButtonClickListener = listener;
return this;
}
/**
* Set the negative button text and it's listener
*
* @param RightButtonText
* @param listener
* @return
*/
public Builder setNegativeButton(String RightButtonText,
DialogInterface.OnClickListener listener) {
this.RightButtonText = RightButtonText;
this.negativeButtonClickListener = listener;
return this;
}
/**
* Create the custom dialog
*/
public BaseDialog create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
final BaseDialog dialog = new BaseDialog(context,
R.style.Dialog);
// 点击弹窗以外的地方消失true,false不消失
dialog.setCanceledOnTouchOutside(false);
View layout = inflater.inflate(R.layout.base_dialog_layout, null);
dialog.addContentView(layout, new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView tv_title= (TextView) layout.findViewById(R.id.title);
LinearLayout dialog_lin=(LinearLayout) layout.findViewById(R.id.dialog_lin);
ImageView iv_image=(ImageView) layout.findViewById(R.id.iv_image);
TextView LeftButton =(TextView) layout.findViewById(R.id.LeftButton);
TextView RightButton =(TextView) layout.findViewById(R.id.RightButton);
TextView tv_xian = (TextView) layout.findViewById(R.id.tv_xian);
TextView tv_message = (TextView) layout.findViewById(R.id.message);
LinearLayout content=(LinearLayout) layout.findViewById(R.id.content);
//设置标题
if (APP.NotNull(title)) {
tv_title.setText(title);
} else {
dialog_lin.setVisibility(View.GONE);
}
//设置图片
if (Image < 0) {//
iv_image.setVisibility(View.GONE);
} else {
Drawable drawable = context.getResources().getDrawable(Image);
iv_image.setImageDrawable(drawable);
}
// set the confirm button
if (LeftButtonText != null) {
LeftButton.setText(LeftButtonText);
if (positiveButtonClickListener != null) {
LeftButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(
dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
LeftButton.setVisibility(View.GONE);
tv_xian.setVisibility(View.GONE);
}
// set the cancel button
if (RightButtonText != null) {
RightButton.setText(RightButtonText);
if (negativeButtonClickListener != null) {
RightButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
negativeButtonClickListener.onClick(
dialog,
DialogInterface.BUTTON_NEGATIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
RightButton.setVisibility(View.GONE);
tv_xian.setVisibility(View.GONE);
}
// set the content message
if (message != null) {
tv_message.setText(message);
}
if (contentView != null) {
// if no message set
// add the contentView to the dialog body
content.removeAllViews();
content.addView(contentView,
new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
dialog.setContentView(layout);
return dialog;
}
}
以上就是主要全部代码的实现了,博主初入android,所以嘿嘿嘿,基本都是比较简单的东西啦,就不做解释了,这里顺便贴一下布局的实现
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@null"
android:minWidth="280dip"
android:orientation="vertical">
<LinearLayout
android:id="@+id/dialog_lin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_title_custom_dialog"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="10dip"
android:paddingTop="10dip">
<ImageView
android:id="@+id/iv_image"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:contentDescription="@string/line"
android:src="@mipmap/ic_expand" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="@color/black"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_middle_custom_dialog"
android:gravity="center"
android:minHeight="100dip"
android:orientation="vertical">
<TextView
android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/item_title_normal"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:lineSpacingMultiplier="1.2"
android:autoLink="all"
android:textSize="16sp" />
<!-- android:lineSpacingMultiplier="1.5"
行间距原来的倍数
-->
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="@color/bg_gray" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_bottom_custom_dialog"
android:orientation="horizontal">
<!--
android:paddingBottom="5dip"
android:paddingTop="5dip"
-->
<TextView
android:id="@+id/LeftButton"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:background="@drawable/set_bbuton_dialog"
android:clickable="true"
android:textColor="@color/mask_tags_3"
android:gravity="center"
android:singleLine="true"
android:textSize="16sp" />
<!-- android:textColor="@color/white"
android:layout_marginRight="10dip"
android:background="@drawable/bbuton_info_rounded"
-->
<TextView
android:id="@+id/tv_xian"
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="@color/bg_gray" />
<TextView
android:id="@+id/RightButton"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:background="@drawable/set_bbuton_dialog"
android:clickable="true"
android:textColor="@color/black"
android:gravity="center"
android:singleLine="true"
android:textSize="18sp" />
<!-- android:background="@drawable/bbuton_danger_rounded"
android:textColor="@color/white"
android:layout_marginLeft="10dip"
-->
</LinearLayout>
</LinearLayout>
到这里就差不多结束了 drawable这里就不贴了,我瞅瞅demo怎么上传把
另外需要学习小伙伴可以加群!大家一起讨论哈哈!一起开车!我在里面只是一个小小的萌新。群号:188089649!转载请注明出处!谢谢!
Demo:仿ios提示弹出框
仿ios各种弹窗实现
下面,介绍一下各种ios弹窗的实现:是借鉴自一位大神的,嘿嘿嘿嘿
这是大神的博客地址,大家可以去看看](http://m.blog.csdn.net/article/details?id=47058159)
话不多说 先开车
先上效果图,本来是想上一直动态图的 ,奈何萌新不会!!!!被我金子一喷,会了。。。
效果图
接下来简单贴一下代码
ActionSheetDialog.java
public class ActionSheetDialog {
private Context context;
private Dialog dialog;
private TextView txt_title;
private TextView txt_cancel;
private LinearLayout lLayout_content;
private ScrollView sLayout_content;
private boolean showTitle = false;
private List<SheetItem> sheetItemList;
private Display display;
public ActionSheetDialog(Context context) {
this.context = context;
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
display = windowManager.getDefaultDisplay();
}
public ActionSheetDialog builder() {
// 获取Dialog布局
View view = LayoutInflater.from(context).inflate(
R.layout.view_actionsheet, null);
// 设置Dialog最小宽度为屏幕宽度
view.setMinimumWidth(display.getWidth());
// 获取自定义Dialog布局中的控件
sLayout_content = (ScrollView) view.findViewById(R.id.sLayout_content);
lLayout_content = (LinearLayout) view
.findViewById(R.id.lLayout_content);
txt_title = (TextView) view.findViewById(R.id.txt_title);
txt_cancel = (TextView) view.findViewById(R.id.txt_cancel);
txt_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
// 定义Dialog布局和参数
dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
dialog.setContentView(view);
Window dialogWindow = dialog.getWindow();
dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.x = 0;
lp.y = 0;
dialogWindow.setAttributes(lp);
return this;
}
public ActionSheetDialog setTitle(String title) {
showTitle = true;
txt_title.setVisibility(View.VISIBLE);
txt_title.setText(title);
return this;
}
public ActionSheetDialog setCancelable(boolean cancel) {
dialog.setCancelable(cancel);
return this;
}
public ActionSheetDialog setCanceledOnTouchOutside(boolean cancel) {
dialog.setCanceledOnTouchOutside(cancel);
return this;
}
/**
*
* @param strItem
* 条目名称
* @param color
* 条目字体颜色,设置null则默认蓝色
* @param listener
* @return
*/
public ActionSheetDialog addSheetItem(String strItem, SheetItemColor color,
OnSheetItemClickListener listener) {
if (sheetItemList == null) {
sheetItemList = new ArrayList<SheetItem>();
}
sheetItemList.add(new SheetItem(strItem, color, listener));
return this;
}
/** 设置条目布局 */
private void setSheetItems() {
if (sheetItemList == null || sheetItemList.size() <= 0) {
return;
}
int size = sheetItemList.size();
// TODO 高度控制,非最佳解决办法
// 添加条目过多的时候控制高度
if (size >= 7) {
LinearLayout.LayoutParams params = (LayoutParams) sLayout_content
.getLayoutParams();
params.height = display.getHeight() / 2;
sLayout_content.setLayoutParams(params);
}
// 循环添加条目
for (int i = 1; i <= size; i++) {
final int index = i;
SheetItem sheetItem = sheetItemList.get(i - 1);
String strItem = sheetItem.name;
SheetItemColor color = sheetItem.color;
final OnSheetItemClickListener listener = (OnSheetItemClickListener) sheetItem.itemClickListener;
TextView textView = new TextView(context);
textView.setText(strItem);
textView.setTextSize(18);
textView.setGravity(Gravity.CENTER);
// 背景图片
if (size == 1) {
if (showTitle) {
textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
} else {
textView.setBackgroundResource(R.drawable.actionsheet_single_selector);
}
} else {
if (showTitle) {
if (i >= 1 && i < size) {
textView.setBackgroundResource(R.drawable.actionsheet_middle_selector);
} else {
textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
}
} else {
if (i == 1) {
textView.setBackgroundResource(R.drawable.actionsheet_top_selector);
} else if (i < size) {
textView.setBackgroundResource(R.drawable.actionsheet_middle_selector);
} else {
textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
}
}
}
// 字体颜色
if (color == null) {
textView.setTextColor(Color.parseColor(SheetItemColor.Blue
.getName()));
} else {
textView.setTextColor(Color.parseColor(color.getName()));
}
// 高度
float scale = context.getResources().getDisplayMetrics().density;
int height = (int) (45 * scale + 0.5f);
textView.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, height));
// 点击事件
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(index);
dialog.dismiss();
}
});
lLayout_content.addView(textView);
}
}
public void show() {
setSheetItems();
dialog.show();
}
public interface OnSheetItemClickListener {
void onClick(int which);
}
public class SheetItem {
String name;
OnSheetItemClickListener itemClickListener;
SheetItemColor color;
public SheetItem(String name, SheetItemColor color,
OnSheetItemClickListener itemClickListener) {
this.name = name;
this.color = color;
this.itemClickListener = itemClickListener;
}
}
public enum SheetItemColor {
Blue("#037BFF"), Red("#FD4A2E");
private String name;
private SheetItemColor(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
2.AlertDialog.java
public class AlertDialog {
private Context context;
private Dialog dialog;
private LinearLayout lLayout_bg;
private TextView txt_title;
private TextView txt_msg;
private Button btn_neg;
private Button btn_pos;
private ImageView img_line;
private Display display;
private boolean showTitle = false;
private boolean showMsg = false;
private boolean showPosBtn = false;
private boolean showNegBtn = false;
public AlertDialog(Context context) {
this.context = context;
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
display = windowManager.getDefaultDisplay();
}
public AlertDialog builder() {
// 获取Dialog布局
View view = LayoutInflater.from(context).inflate(
R.layout.view_alertdialog, null);
// 获取自定义Dialog布局中的控件
lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg);
txt_title = (TextView) view.findViewById(R.id.txt_title);
txt_title.setVisibility(View.GONE);
txt_msg = (TextView) view.findViewById(R.id.txt_msg);
txt_msg.setVisibility(View.GONE);
btn_neg = (Button) view.findViewById(R.id.btn_neg);
btn_neg.setVisibility(View.GONE);
btn_pos = (Button) view.findViewById(R.id.btn_pos);
btn_pos.setVisibility(View.GONE);
img_line = (ImageView) view.findViewById(R.id.img_line);
img_line.setVisibility(View.GONE);
// 定义Dialog布局和参数
dialog = new Dialog(context, R.style.AlertDialogStyle);
dialog.setContentView(view);
// 调整dialog背景大小
lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
.getWidth() * 0.85), LayoutParams.WRAP_CONTENT));
return this;
}
public AlertDialog setTitle(String title) {
showTitle = true;
if ("".equals(title)) {
txt_title.setText("标题");
} else {
txt_title.setText(title);
}
return this;
}
public AlertDialog setMsg(String msg) {
showMsg = true;
if ("".equals(msg)) {
txt_msg.setText("内容");
} else {
txt_msg.setText(msg);
}
return this;
}
public AlertDialog setCancelable(boolean cancel) {
dialog.setCancelable(cancel);
return this;
}
public AlertDialog setPositiveButton(String text,
final OnClickListener listener) {
showPosBtn = true;
if ("".equals(text)) {
btn_pos.setText("确定");
} else {
btn_pos.setText(text);
}
btn_pos.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(v);
dialog.dismiss();
}
});
return this;
}
public AlertDialog setNegativeButton(String text,
final OnClickListener listener) {
showNegBtn = true;
if ("".equals(text)) {
btn_neg.setText("取消");
} else {
btn_neg.setText(text);
}
btn_neg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(v);
dialog.dismiss();
}
});
return this;
}
private void setLayout() {
if (!showTitle && !showMsg) {
txt_title.setText("提示");
txt_title.setVisibility(View.VISIBLE);
}
if (showTitle) {
txt_title.setVisibility(View.VISIBLE);
}
if (showMsg) {
txt_msg.setVisibility(View.VISIBLE);
}
if (!showPosBtn && !showNegBtn) {
btn_pos.setText("确定");
btn_pos.setVisibility(View.VISIBLE);
btn_pos.setBackgroundResource(R.drawable.alertdialog_single_selector);
btn_pos.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
if (showPosBtn && showNegBtn) {
btn_pos.setVisibility(View.VISIBLE);
btn_pos.setBackgroundResource(R.drawable.alertdialog_right_selector);
btn_neg.setVisibility(View.VISIBLE);
btn_neg.setBackgroundResource(R.drawable.alertdialog_left_selector);
img_line.setVisibility(View.VISIBLE);
}
if (showPosBtn && !showNegBtn) {
btn_pos.setVisibility(View.VISIBLE);
btn_pos.setBackgroundResource(R.drawable.alertdialog_single_selector);
}
if (!showPosBtn && showNegBtn) {
btn_neg.setVisibility(View.VISIBLE);
btn_neg.setBackgroundResource(R.drawable.alertdialog_single_selector);
}
}
public void show() {
setLayout();
dialog.show();
}
}
xml在这里就不贴了,需要的可以去原作者那里下载。
另外需要学习小伙伴可以加群!大家一起讨论哈哈!一起开车!我在里面只是一个小小的萌新。群号:188089649!
第一次,勿碰勿怪**