浅析Android中常见三种弹框在项目中的应用
程序员文章站
2023-12-03 08:07:52
一丶概述
弹框在android项目中经常出现,常见的实现方法有三种:dialog 弹框,window弹框,activity伪弹框。本文就说一说三种弹框的实现及在项目中的运...
一丶概述
弹框在android项目中经常出现,常见的实现方法有三种:dialog 弹框,window弹框,activity伪弹框。本文就说一说三种弹框的实现及在项目中的运用。
二丶演示图
图一为常见的三种弹框(文末上链接),图二为项目中用到的activity伪弹框
三丶正文
1.dialog弹框
先看一篇一篇文章:
android 8种对话框(dialog)使用方法汇总
dialog是系统自带的弹框,然而常常因为ui不好看而遭嫌弃,常需要自定义
public class mydialog extends dialog implements android.view.view.onclicklistener { private context mcontext; private string mtitle; private string mdetail; private textview mtexttitle; private textview mtextdetail; private textview mbuttonyes; private textview mbuttonno; private onclickinterface monclclickinterface; public mydialog(context context, string title, string detail) { super(context, r.style.mydialogstyle); this.mcontext = context; this.mtitle = title; this.mdetail = detail; } @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); initview(); } private void initview() { layoutinflater inflater = layoutinflater.from(mcontext); view view = inflater.inflate(r.layout.layout_dialog, null); setcontentview(view); mbuttonyes = (textview) view.findviewbyid(r.id.dialog_yes); mbuttonno = (textview) view.findviewbyid(r.id.dialog_no); mtexttitle = (textview) view.findviewbyid(r.id.dialog_title); mtextdetail = (textview) view.findviewbyid(r.id.dialog_detail); mtexttitle.settext(mtitle); mtextdetail.settext(mdetail); mbuttonyes.setonclicklistener(this); mbuttonno.setonclicklistener(this); } public interface onclickinterface { public void clickyes(); public void clickno(); } public void setonclickinterface(onclickinterface onclclickinterface) { this.monclclickinterface = onclclickinterface; } @override public void onclick(view v) { switch (v.getid()) { case r.id.dialog_yes: monclclickinterface.clickyes(); break; case r.id.dialog_no: monclclickinterface.clickno(); break; default: break; } } }
这里就是:1.绑定布局 2.设置监听,监听接口
主要说一下布局里的stytle(悬浮在activity上,模糊显示就是在这里控制)
<style name="mydialogstyle"> <item name="android:windowbackground">@android:color/transparent</item> <!--设置dialog的背景--> <item name="android:windowframe">@null</item> <!--dialog的windowframe框为无--> <item name="android:windownotitle">true</item> <!--是否有title--> <item name="android:windowisfloating">true</item> <!--是否浮现在activity之上--> <item name="android:windowistranslucent">false</item> <!--是否半透明--> <item name="android:windowcontentoverlay">@null</item> <!--对话框是否有遮盖,这个不设置的话,可能会出现边框黑线--> <item name="android:windowanimationstyle">@android:style/animation.dialog</item> <!--动画--> <item name="android:backgrounddimenabled">true</item> <!-- 背景是否模糊显示--> </style>
dialog弹框一般用于,网络数据加载显示,或交互较少的弹框(基本被新下拉刷新控件swiperefreshlayout替代)
2.window弹框
public class mywindow { private context mcontext; private windowmanager mwinwindowmanager; private view mview; private static boolean isshow = false; public mywindow(context context) { mcontext = context.getapplicationcontext(); } public void showmywindow() { if (isshow) { return; } mwinwindowmanager = (windowmanager) mcontext.getsystemservice(context.window_service); //设置windowmanager.layoutparams的属性 windowmanager.layoutparams params = new windowmanager.layoutparams(); //类型 params.type = windowmanager.layoutparams.type_system_alert; //flags //如果设置了windowmanager.layoutparams.flag_not_focusable,弹出的view没焦点,收不到back键的事件 //当按back、home键时,背景应用退出,弹出的view就可以悬浮在桌面了。 params.flags = windowmanager.layoutparams.flag_alt_focusable_im; params.format = pixelformat.translucent; params.width = layoutparams.match_parent; params.height = layoutparams.match_parent; params.gravity = gravity.center; //初始化view mview = initview(mcontext); //点击back键,关闭window mview.setonkeylistener(new view.onkeylistener() { @override public boolean onkey(view v, int keycode, keyevent event) { log.d("wxx", "onkey"); switch (keycode) { case keyevent.keycode_back: log.d("wxx", "onkey back"); hidemywindow(); return true; default: return false; } } }); mwinwindowmanager.addview(mview, params); isshow = true; } private view initview(context context) { layoutinflater inflater = layoutinflater.from(context); view view = inflater.inflate(r.layout.layout_window, null); button btnyes = (button) view.findviewbyid(r.id.window_yes); btnyes.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mcontext, "window yes!", toast.length_long).show(); hidemywindow(); } }); button btnno = (button) view.findviewbyid(r.id.window_no); btnno.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mcontext, "window no!", toast.length_long).show(); hidemywindow(); } }); //点击window窗口外围,关闭window final view wview = view.findviewbyid(r.id.view_layout); view.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { int x = (int) event.getx(); int y = (int) event.gety(); rect rect = new rect(); wview.getglobalvisiblerect(rect); if (!rect.contains(x, y)) { hidemywindow(); } return false; } }); return view; } public void hidemywindow() { if (isshow && mview != null) { mwinwindowmanager.removeview(mview); isshow = false; } } }
这个就厉害了,展现,隐藏,点击监听都是自己写的方法,原来点击窗口外关闭窗口是这样实现的,是不是又学到了
然后是关于:
android window windowmanager 整理
主要用于自定义控件,比如说下拉框:
自定义spinner下拉框
3.activity伪弹框
public class myactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.layout_activity); } @override public boolean ontouchevent(motionevent event) { this.finish(); return true; } public void yesbutton(view v) { this.finish(); toast.maketext(getapplicationcontext(), "yes, hunman is activity", toast.length_long).show(); } public void nobutton(view v) { this.finish(); toast.maketext(getapplicationcontext(), "no, hunman is not activity", toast.length_long).show(); } }
简单得不能再简单,跳转跳转到另一个activity
注意点activity主题(控制activity为弹框样式):
<activity android:name="com.wuxianxi.hunman.smallwindows.myactivity" android:theme="@style/myactivitystyle" > </activity> <style name="myactivitystyle"> <item name="android:windowbackground">@android:color/transparent</item> <!--设置dialog的背景--> <item name="android:windowframe">@null</item> <!--dialog的windowframe框为无--> <item name="android:windownotitle">true</item> <!--是否有title--> <item name="android:windowisfloating">true</item> <!--是否浮现在activity之上--> <item name="android:windowistranslucent">true</item> <!--是否半透明--> <item name="android:windowcontentoverlay">@null</item> <!--对话框是否有遮盖,这个不设置的话,可能会出现边框黑线--> <item name="android:windowanimationstyle">@android:style/animation.dialog</item> <!--动画--> <item name="android:backgrounddimenabled">true</item> <!-- 背景是否模糊显示--> </style>
activity伪弹框最常用,用于交互,操作较复杂的弹框,如图二
补充这里可通过方法控制activity弹框宽比
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); /** 设置宽度为屏幕的0.9*/ windowmanager windowmanager = getwindowmanager(); /* 获取屏幕宽、高 */ display display = windowmanager.getdefaultdisplay(); /* 获取对话框当前的参数值 */ windowmanager.layoutparams p = getwindow().getattributes(); /* 宽度设置为屏幕的0.9 */ p.width = (int) (display.getwidth() * 0.9); /* 设置透明度,0.0为完全透明,1.0为完全不透明 */ p.alpha = 0.95f; /* 设置布局参数 */ getwindow().setattributes(p); // getwindow().setlayout(viewgroup.layoutparams.match_parent, // viewgroup.layoutparams.wrap_content); /* 设置点击弹框外部不可消失 */ setfinishontouchoutside(false); }
最后贴上mainactivity
public class mainactivity extends actionbaractivity implements onclicklistener { private button btnwindow; private button btndialog; private button btnactivity; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initview(); } private void initview() { btnwindow = (button) findviewbyid(r.id.btn_window); btndialog = (button) findviewbyid(r.id.btn_dialog); btnactivity = (button) findviewbyid(r.id.btn_activity); btnwindow.setonclicklistener(this); btndialog.setonclicklistener(this); btnactivity.setonclicklistener(this); } @override public void onclick(view v) { switch (v.getid()) { case r.id.btn_window: mywindow mywindow = new mywindow(this); mywindow.showmywindow(); break; case r.id.btn_dialog: //注意下面第一个参数不能为getapplicationcontext(),而应该是activity, 因为办有activity才能添加窗口 final mydialog dialog = new mydialog(mainactivity.this, "hunman - dialog", "hunman is a dialog\nyes or no!"); dialog.show(); dialog.setonclickinterface(new mydialog.onclickinterface() { @override public void clickyes() { dialog.dismiss(); toast.maketext(getapplicationcontext(), "yes, hunman is dialog", toast.length_long).show(); } @override public void clickno() { dialog.dismiss(); toast.maketext(getapplicationcontext(), "yes, hunman is not dialog", toast.length_long).show(); } }); break; case r.id.btn_activity: intent intent3 = new intent(mainactivity.this, myactivity.class); startactivity(intent3); break; default: break; } } }
代码下载地址:https://github.com/babywu/smallwindows
以上所述是小编给大家介绍的浅析android中常见三种弹框在项目中的应用,希望对大家有所帮助