Android 开发使用PopupWindow实现弹出警告框的复用类示例
程序员文章站
2022-11-23 11:57:44
本文实例讲述了android 开发使用popupwindow实现弹出警告框的复用类。分享给大家供大家参考,具体如下:android开发中相信下图所示界面大家都不陌生,该种弹出框的使用频率也是极高的,所...
本文实例讲述了android 开发使用popupwindow实现弹出警告框的复用类。分享给大家供大家参考,具体如下:
android开发中相信下图所示界面大家都不陌生,该种弹出框的使用频率也是极高的,所以我专门谢了个类用于方便的弹出该界面。并把确定或取消后的逻辑通过抽象方法的方式让用户自己实现,大大提高了开发效率。下面是该类:
package com.***.popupwindow; import ******; public abstract class mypopupwindow { private popupwindow popupwindow; private activity context; private string content; private string positiveword = "确定"; private string negativeword = "取消"; /** * 构造函数 * * @param context */ public mypopupwindow(activity context) { this.context = context; } /** * 显示警示框 */ public void show() { view popview = view.inflate(context, r.layout.popup, null); popupwindow = new popupwindow(context); popupwindow.setheight(400); popupwindow.setwidth(700); popupwindow.setoutsidetouchable(true); popupwindow.setfocusable(true); popupwindow.setcontentview(popview); popupwindow.showatlocation(context.getwindow().getdecorview(), gravity.center, 0, 0); textview tv_pop_text = (textview) popview.findviewbyid(r.id.tv_pop_text); tv_pop_text.settext(content); button bt_pop_sure = (button) popview.findviewbyid(r.id.bt_pop_sure); bt_pop_sure.settext(positiveword); bt_pop_sure.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { sureclick(); } }); button bt_pop_cancel = (button) popview.findviewbyid(r.id.bt_pop_cancel); bt_pop_cancel.settext(negativeword); bt_pop_cancel.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { cancelclick(); } }); } /** * 确定键按下后执行 */ public abstract void sureclick(); /** * 取消键按下后执行 */ public abstract void cancelclick(); /** * 为警示设置警示内容 * * @param content */ public void setcontent(string content) { this.content = content; } /** * 设置确定键文字 * * @param positiveword */ public void setpositiveword(string positiveword) { this.positiveword = positiveword; } /** * 设置取消键文字 * * @param negativeword */ public void setnegativeword(string negativeword) { this.negativeword = negativeword; } /** * 手动取消警示框 */ public void dismiss() { popupwindow.dismiss(); } }
其中弹出框用到的布局popup.xml代码如下:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:orientation="vertical"> <textview android:id="@+id/tv_pop_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center"/> <textview android:layout_width="match_parent" android:layout_height="1px" android:background="@android:color/darker_gray"/> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <button android:id="@+id/bt_pop_sure" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:layout_weight="1"/> <textview android:layout_width="1px" android:layout_height="match_parent" android:background="@android:color/darker_gray"/> <button android:id="@+id/bt_pop_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:layout_weight="1"/> </linearlayout> </linearlayout>
下面简单的使用一下:在界面放一个按钮,按钮点击后弹出警告框。代码如下:
package com.toprs.popupwindow; import android.graphics.color; import android.graphics.drawable.colordrawable; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.attributeset; import android.view.gravity; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.button; import android.widget.listview; import android.widget.popupwindow; import android.widget.seekbar; import android.widget.toast; public class mainactivity extends appcompatactivity { private popupwindow popupwindow; private button button; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button = (button) findviewbyid(r.id.button); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { mypopupwindow mypopupwindow = new mypopupwindow(mainactivity.this) { @override public void sureclick() { toast.maketext(mainactivity.this, "确定", toast.length_short).show(); } @override public void cancelclick() { toast.maketext(mainactivity.this, "取消", toast.length_short).show(); } }; mypopupwindow.setcontent("确定退出?"); mypopupwindow.show(); } }); } }
即如下效果:
so,以后使用只需要简单调用几句代码就好了!
上一篇: 酸奶水果捞非常美味,教你花样做法