popupwindow的封装

本文介绍了在项目中为了提高效率对PopupWindow进行封装的方法,使得在Activity中的调用更加简便。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于在项目中会经常用的popupwindows所以对其进行了封装。这样用起来就比较方便
 我想在Activity中这样使用 :   
MyPopupWindows myPopupWindows = new MyPopupWindows(this,R.layout.layout_myPopupWindows);//R.layout.layout_myPopupWindows 布局文件

/**
*对布局文件中的控件进行初始化和添加事件监听
*/
myPopupWindows.setMyCallBack(new MyPopupWindows.MyCallBack() {
    @Override
    public void func(View view) {
        //对布局文件进行初始化
        TextView tv = view.findViewById(R.id.tv);
        //对布局中的控件添加事件监听
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
            }
        });
        
    }
});
/*
*显示该popupwindows
isShowAsDropDown:表示是否显示在activity某控件的下面
parent:activity布局中的控件。
width:该popupwindows显示的宽度 
height:该popupwindows显示的高度
*/
myPopupWindows.show(boolean isShowAsDropDown, View parent, int width, int height)
/**
*退出该popupwindows
*/
myPopupWindows.dismiss();

下面上封装的代码:
package com.lyt.project.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;

/**
 * Created by liyunte on 2016/7/21.
 * 使用方法:
 * 初始化MyPopupWindows;
 * 设置监听并初始化控件  setMyCallBack
 * 显示  show
 * 隐藏 dismiss(); 由于可直接调用MyPopupWindows的父类PopupWindow的dismiss();
 * 所以此类中不重写dismiss();
 *
 */
public class MyPopupWindows extends PopupWindow {
    private Activity context;//
    private LayoutInflater inflater;
    private View contentView;//myPopupWindows所在的布局
    private final int h;//显示的高度
    private final int w;//显示的宽度
    private MyCallBack myCallBack;//用来初始化mypopupwindows布局中的控件和添加事件监听

    public MyPopupWindows(Activity context,int layout){
        this.context = context;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//获取inflater
        contentView = inflater.inflate(layout, null);//加载布局文件并保存在View中
        h = context.getWindowManager().getDefaultDisplay().getHeight();//获取屏幕的高度并赋值给h
        w = context.getWindowManager().getDefaultDisplay().getWidth();//获取屏幕的宽度并赋值给w
        this.setContentView(contentView);//添加到popupwindows中
        this.setFocusable(true);//设置可获取焦点
        this.setOutsideTouchable(true);//设置外部可触摸
        this.update();
        this.setBackgroundDrawable(new ColorDrawable());//必须设置背景否则报错
        /*
        * 触摸拦截
        * */
        this.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });

    }

    /*
    *isShowAsDropDown :true显示在parent控件的下面 false:显示在屏幕中间
    * parent:activity布局中的控件
    * width:显示的宽度 
    * height;显示的高度
    * */
    public void show(boolean isShowAsDropDown, View parent, int width, int height) {
        /*
        判断接口是否为空,不为空则和绑定事件监听
        * */
        if (myCallBack != null) {
            myCallBack.func(contentView);
        }
        /*
        * 如果形参width和height小于或等于0则用获取到的屏幕宽高
        * */
        if (width <= 0 || height <= 0) {
            this.setWidth(w);
            this.setHeight(h);
        } else {
            this.setWidth(width);
            this.setHeight(height);
        }
        /*
        * isShowAsDropDown :true显示在parent控件的下面 false:显示在屏幕中间
        * */
        if (isShowAsDropDown) {
            this.showAsDropDown(parent);
        } else {
            this.showAtLocation(parent, Gravity.CENTER, 0, 0);
        }


    }

    public void setMyCallBack(MyCallBack myCallBack){
        this.myCallBack = myCallBack;
    }
    public interface MyCallBack{
        public void func(View view);
    }


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值