Android:PopupWindow工具类,调用几行代码搞定
来源:互联网 发布:如何利用网络推广 编辑:程序博客网 时间:2024/06/03 20:07
一、前言
最近写项目,老是碰到PopupWindow,于是每次都写一大堆的代码,后来想想能不能封装一下,方便调用,然后就抽空写了一个万能通用的工具类。废话少说,直接上代码。
二、代码部分
package com.aomei.myexample.Utils;import android.app.Activity;import android.content.Context;import android.graphics.drawable.ColorDrawable;import android.util.SparseArray;import android.view.LayoutInflater;import android.view.View;import android.view.WindowManager;import android.widget.ImageView;import android.widget.PopupWindow;import android.widget.TextView;/** * 封装的PopupWidow的工具类 * * @author hf 2016-09-09 13:31 * @version 1.0 * @des * @版本 $Rev$ * @change $Author$ $Date$ * @des 用英文装个逼不容易啊--调用很简单,直接new完传递参数,实现方法,通过builder调用方法,可以自己扩展 */public abstract class PopUtils { private Context mContext; private int mlayoutResId; private int mHeight; private int mwidth; private ClickListener mCallback; /** * The constructor of MyPopupWindow * * @param context Interface to global information about an application environment * @param layoutResId The resource id of the layout embedded inside the PopupWindow * @param width the width of the PopupWindow * @param height the height of the PopupWindow * @param view a parent view to get the android.view.View.getWindowToken() token from * @param gravity the gravity which controls the placement of the popup window * @param x the PopupWindow's x location offset * @param y the PopupWindow's y location offset */ public PopUtils(Context context, int layoutResId, int width, int height, View view, int gravity, int x, int y, ClickListener callback) { this.mContext = context; this.mlayoutResId = layoutResId; this.mwidth = width; this.mHeight = height; setCallBack(callback); PopBuilder builder = PopBuilder.createPopupWindow(context, layoutResId, width, height, view, gravity, x, y, mCallback); } public static class PopBuilder { private ClickListener mCallback; private static PopupWindow window; private SparseArray<View> mViews; private View mItem; private PopBuilder(Context context, View view, ClickListener callback) { this.mViews = new SparseArray<>(); this.mItem = view; this.mCallback = callback; } public static PopBuilder createPopupWindow(final Context context, int layoutResId, int width, int height, View parent, int gravity, int x, int y, ClickListener callback) { // 利用layoutInflater获得View LayoutInflater inflater = (LayoutInflater) ((Activity) context) .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(layoutResId, null); PopBuilder builder = new PopBuilder(context, view, callback); window = new PopupWindow(view, width, height); // 设置popWindow弹出窗体可点击,这句话必须添加,并且是true window.setFocusable(true); window.setTouchable(true); // 设置触摸外面时消失 window.setOutsideTouchable(true); window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); // 监听PopupWindow关闭,如果为关闭状态则设置为空 window.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { window = null; // 主界面完全显示 WindowManager.LayoutParams params = ((Activity) context).getWindow().getAttributes(); params.alpha = 1.0f; ((Activity) context).getWindow().setAttributes(params); } }); // 实例化一个ColorDrawable颜色为透明,不设置为半透明是因为带圆角 ColorDrawable dw = new ColorDrawable(context.getResources().getColor(android.R.color.transparent)); window.setBackgroundDrawable(dw); window.showAtLocation(parent, gravity, x, y); // 主界面变暗 WindowManager.LayoutParams params = ((Activity) context).getWindow().getAttributes(); params.alpha = 0.4f; ((Activity) context).getWindow().setAttributes(params); //点击事件回调 if (window != null) { callback.setUplistener(builder); } return builder; } /** * 得到视图 * * @param id 控件资源id * @param <T> 类型 * * @return T */ public <T extends View> T getView(int id) { T t = (T) mViews.get(id); if (t == null) { t = (T) mItem.findViewById(id); mViews.put(id, t); } return t; } /** * 使窗口消失 * @return */ public PopBuilder dismiss() { if (window != null) { window.dismiss(); } return this; } /** * 设置是否可见 * * @param id 控件id * @param visibility 是否可见 * * @return PopBuilder */ public PopBuilder setVisibility(int id, int visibility) { getView(id).setVisibility(visibility); return this; } /** * 设置图片资源 * * @param id 控件id * @param drawableRes drawable资源id * * @return PopBuilder */ public PopBuilder setImageResource(int id, int drawableRes) { View view = getView(id); if (view instanceof ImageView) { ((ImageView) view).setImageResource(drawableRes); } else { view.setBackgroundResource(drawableRes); } return this; } /** * 设置文本 * * @param id 控件id * @param text 文本内容 * * @return PopBuilder */ public PopBuilder setText(int id, CharSequence text) { View view = getView(id); if (view instanceof TextView) { ((TextView) view).setText(text); } return this; } } /** * 用于回调的接口 */ public interface ClickListener { void setUplistener(PopBuilder builder); } /** * 设置回调对象 * @param callBack 回调对象 */ private void setCallBack(ClickListener callBack) { this.mCallback = callBack; }}
三、调用示例
PopUtils utils = new PopUtils(ThirdActivity.this, R.layout.activity_sqlite, 600, 400, mBtnSend, Gravity.CENTER, 0, 0, new PopUtils.ClickListener() { @Override public void setUplistener(final PopUtils.PopBuilder builder) { builder.setText(R.id.btn_sqlite_find_all, "查询"); builder.getView(R.id.btn_sqlite_find_all).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { builder.dismiss(); } }); } }) { };
0 0
- Android:PopupWindow工具类,调用几行代码搞定
- 通用PopupWindow,几行代码搞定PopupWindow弹窗
- 通用PopupWindow,几行代码搞定PopupWindow弹窗(续)
- 通用PopupWindow,几行代码搞定PopupWindow弹窗
- 对PopupWindow常用API的简单封装,几行代码就搞定PopupWindow弹窗,使用Builder模式,链式调用,像使用AlertDialog 一样
- 几行代码搞定二级域名!!!!!!
- 几行代码搞定Android底部导航栏
- 超简单,几行代码搞定Android底部导航栏
- thinkphp实现分页类 几行代码搞定
- 几行代码搞定nodejs中的回调坑
- 几行代码搞定画廊效果
- Vue几行代码搞定省市联动
- android 史上最简单的下拉选择菜单DropDownMenu 几行代码轻松搞定!
- android接入微信支付(真的很简单,几行代码就搞定了)
- 底部导航栏:超简单,几行代码搞定Android底部导航栏
- Android懒人专用PopupWindow的工具类
- android 高兼容性的自动填充验证码工具类(四步代码搞定)
- Android沉浸式状态栏工具类,一句代码轻松搞定
- Spring IOC 依赖注入
- 单片机--led
- Linux 开发学习
- Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clan
- android EditText输入框获得焦点时候边框变为变色
- Android:PopupWindow工具类,调用几行代码搞定
- Leetcode 47 Permutations II
- java中的转义字符
- [React Native]Promise机制
- Android下面的MD5加密
- Py第三问 __name__和__main__的使用
- touch 修改文件时间或创建新文件
- js实现显示密码和隐藏密码
- 人件读书笔记(19)在这儿很开心