Android仿IOS自定义AlertDialog提示框
程序员文章站
2022-10-14 14:24:27
本文介绍的仿ios对话框的实现,先来看一下效果图
具体代码如下:
public class alertdialog {
private contex...
本文介绍的仿ios对话框的实现,先来看一下效果图
具体代码如下:
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() { view view = layoutinflater.from(context).inflate( r.layout.view_alertdialog, null); 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 = new dialog(context, r.style.alertdialogstyle); dialog.setcontentview(view); llayout_bg.setlayoutparams(new framelayout.layoutparams((int) (display .getwidth() * 0.80), 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(); } }
布局文件view_alertdialog.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/llayout_bg" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/alert_bg" android:orientation="vertical" > <textview android:id="@+id/txt_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginleft="15dp" android:layout_marginright="15dp" android:layout_margintop="15dp" android:gravity="center" android:textcolor="@color/black" android:textsize="18sp" android:text="提示" android:textstyle="bold" /> <textview android:id="@+id/txt_msg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginleft="15dp" android:layout_marginright="15dp" android:layout_margintop="32dp" android:gravity="center" android:text="提示信息" android:textcolor="@color/black" android:textsize="16sp" /> <imageview android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_margintop="25dp" android:background="@color/alertdialog_line" /> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <button android:id="@+id/btn_neg" android:layout_width="wrap_content" android:layout_height="43dp" android:layout_weight="1" android:background="@drawable/alertdialog_left_selector" android:gravity="center" android:textcolor="@color/actionsheet_blue" android:textsize="16sp" /> <imageview android:id="@+id/img_line" android:layout_width="0.5dp" android:layout_height="43dp" android:background="@color/alertdialog_line" /> <button android:id="@+id/btn_pos" android:layout_width="wrap_content" android:layout_height="43dp" android:layout_weight="1" android:background="@drawable/alertdialog_right_selector" android:gravity="center" android:textcolor="@color/actionsheet_blue" android:textsize="16sp" android:textstyle="bold" /> </linearlayout> </linearlayout>
alertdialog_left_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/alert_btn_left_pressed" android:state_pressed="true"/> <item android:drawable="@drawable/trans_bg"/> </selector>
alertdialog_right_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/alert_btn_right_pressed" android:state_pressed="true"/> <item android:drawable="@drawable/trans_bg"/> </selector>
alertdialog_single_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/alert_btn_single_pressed" android:state_pressed="true"/> <item android:drawable="@drawable/trans_bg"/> </selector> <style name="alertdialogstyle" parent="@android:style/theme.dialog"> <item name="android:windowbackground">@android:color/transparent</item> <item name="android:windowcontentoverlay">@null</item> <item name="android:windowisfloating">true</item> <item name="android:windowframe">@null</item> <item name="android:backgrounddimenabled">true</item> <item name="android:windownotitle">true</item> <item name="android:windowistranslucent">true</item> </style>
用到的.9图片
调用方式:
new alertdialog(this).builder().setmsg(msg) .setpositivebutton("确定", new onclicklistener() { @override public void onclick(view v) { } }).setnegativebutton("取消", new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub } }).show();
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 内容为王的时代,企业如何做好内容营销?