popwindow创建以及事件拦截与内部包含checkbox选中

本文介绍如何使用Android自定义PopUpWindow实现书籍筛选功能,包括界面布局、动画效果及交互处理。通过设置CheckBox来筛选书籍状态和收费状态,并实现点击确定后的逻辑处理。

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

1:需求一个这样的弹窗覆盖
这里写图片描述
2:界面搭建以及popuwidow弹出动画效果

   private void cate_screen_poppu() {
        View view = LayoutInflater.from(this).inflate(R.layout.cate_screen_popup, null);
        final CheckBox checkbox_all = (CheckBox) view.findViewById(R.id.checkbox_all);
        final   CheckBox checkbox_serial = (CheckBox) view.findViewById(R.id.checkbox_serial);
        final  CheckBox checkbox_free = (CheckBox) view.findViewById(R.id.checkbox_free);
        final CheckBox checkbox_vip = (CheckBox) view.findViewById(R.id.checkbox_vip);
        final RelativeLayout rl_checkbox_serial = (RelativeLayout) view.findViewById(R.id.rl_checkbox_serial);
        final RelativeLayout rl_checkbox_all = (RelativeLayout) view.findViewById(R.id.rl_checkbox_all);
        final RelativeLayout rl_checkbox_free = (RelativeLayout) view.findViewById(R.id.rl_checkbox_free);
        final RelativeLayout rl_checkbox_vip = (RelativeLayout) view.findViewById(R.id.rl_checkbox_vip);
        boolean all = PrefUtils.getBoolean(UiUtils.getContext(), "checkbox_all", true);
        if(all){
            checkbox_all.setChecked(true);
        }else{
            checkbox_all.setChecked(false);
        }
        boolean serial = PrefUtils.getBoolean(UiUtils.getContext(), "checkbox_serial", true);
        if(serial){
            checkbox_serial.setChecked(true);
        }else{
            checkbox_serial.setChecked(false);
        }
        boolean free = PrefUtils.getBoolean(UiUtils.getContext(), "checkbox_free", true);
        if(free){
            checkbox_free.setChecked(true);
        }else{
            checkbox_free.setChecked(false);
        }
        boolean vip = PrefUtils.getBoolean(UiUtils.getContext(), "checkbox_vip", true);
        if(vip){
            checkbox_vip.setChecked(true);
        }else{
            checkbox_vip.setChecked(false);
        }
        Button ok_button = (Button) view.findViewById(R.id.ok_button);
        Button resetting_button = (Button) view.findViewById(R.id.resetting_button);

        WindowManager manger = (WindowManager) getSystemService(CatagaryActivity.WINDOW_SERVICE);
        @SuppressWarnings("deprecation")
        int width = manger.getDefaultDisplay().getWidth()/4;
        int heigh = manger.getDefaultDisplay().getHeight();
        mWindow = new PopupWindow(view,3*width, heigh-getStatusBarHeight(this));
        mWindow.setBackgroundDrawable(getResources().getDrawable(android.R.color.transparent));
        mWindow.setOutsideTouchable(true);
        mWindow.setTouchable(true);
       mWindow.setFocusable(true);
        mWindow.update();
        View parent = LayoutInflater.from(this).inflate(R.layout.catagary_activity, null);
        //为popWindow添加动画效果
        mWindow.setAnimationStyle(R.style.popWindow_animation);
        // 点击弹出泡泡窗口
        mWindow.showAtLocation(parent, Gravity.RIGHT, 0,getStatusBarHeight(this));
        //popupWindow在弹窗的时候背景半透明
        final WindowManager.LayoutParams params = getWindow().getAttributes();
        params.alpha = 0.5f;
        getWindow().setAttributes(params);
        mWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                params.alpha = 1.0f;
                getWindow().setAttributes(params);
            }
        });

   //判断是否选中切换书籍筛选状态
        resetting_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox_all.setChecked(true);
                checkbox_serial.setChecked(true);
                checkbox_free.setChecked(true);
                checkbox_vip.setChecked(true);
            }
        });
        ok_button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                boolean checkbox_allSelected = checkbox_all.isChecked();
                Log.e("glk",checkbox_allSelected+"");
                boolean checkbox_serialSelected = checkbox_serial.isChecked();
                boolean checkbox_freeSelected = checkbox_free.isChecked();
                boolean checkbox_vipSelected = checkbox_vip.isChecked();
                if(checkbox_allSelected&&checkbox_serialSelected&&checkbox_freeSelected&&checkbox_vipSelected){
                    radio_cate_screen_book.setTextColor(Color.BLACK);
                }else{
                    radio_cate_screen_book.setTextColor(Color.RED);
                }
                if((checkbox_allSelected&&checkbox_serialSelected&&checkbox_freeSelected&&checkbox_vipSelected)||(!checkbox_allSelected&&!checkbox_serialSelected&&!checkbox_freeSelected&&!checkbox_vipSelected)){
                    mProcess="";
                    mIsVip="";
                    presenter.getBookCategaryList(mCid, mOrder, mProcess, mIsVip, mPage,"10");
                    mAdapter.notifyDataSetChanged();
                }else if(checkbox_allSelected&&!checkbox_serialSelected&&checkbox_freeSelected&&checkbox_vipSelected){
                    mProcess="2";
                    mIsVip="";
                    presenter.getBookCategaryList(mCid, mOrder, mProcess, mIsVip, mPage,"10");
                    mAdapter.notifyDataSetChanged();
                }else if(checkbox_allSelected&&!checkbox_serialSelected&&!checkbox_freeSelected&&checkbox_vipSelected){
                    mProcess="2";
                    mIsVip="0";
                    presenter.getBookCategaryList(mCid, mOrder, mProcess, mIsVip, mPage,"10");
                    mAdapter.notifyDataSetChanged();
                }
                else if(checkbox_allSelected&&!checkbox_serialSelected&&checkbox_freeSelected&&!checkbox_vipSelected){
                    mProcess="2";
                    mIsVip="1";
                    presenter.getBookCategaryList(mCid, mOrder, mProcess, mIsVip, mPage,"10");
                    mAdapter.notifyDataSetChanged();
                }
                else if(!checkbox_allSelected&&checkbox_serialSelected&&checkbox_freeSelected&&checkbox_vipSelected){
                    mProcess="1";
                    mIsVip="";
                    presenter.getBookCategaryList(mCid, mOrder, mProcess, mIsVip, mPage,"10");
                    mAdapter.notifyDataSetChanged();
                }else if(!checkbox_allSelected&&checkbox_serialSelected&&!checkbox_freeSelected&&checkbox_vipSelected){
                    mProcess="1";
                    mIsVip="0";
                    presenter.getBookCategaryList(mCid, mOrder, mProcess, mIsVip, mPage,"10");
                    mAdapter.notifyDataSetChanged();
                }else if(!checkbox_allSelected&&checkbox_serialSelected&&checkbox_freeSelected&&!checkbox_vipSelected){
                    mProcess="1";
                    mIsVip="1";
                    presenter.getBookCategaryList(mCid, mOrder, mProcess, mIsVip, mPage,"10");
                    mAdapter.notifyDataSetChanged();
                }

                mWindow.dismiss();
            }

        });
        checkbox_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton button, boolean b) {
                checkbox_all.setChecked(b? true:false);
                PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_all",b);
            }
        });
        checkbox_serial.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton button, boolean b) {
                checkbox_serial.setChecked(b? true:false);
                PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_serial",b);
            }
        });
        checkbox_free.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton button, boolean b) {
                checkbox_free.setChecked(b? true:false);
                PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_free",b);
            }
        });
        checkbox_vip.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton button, boolean b) {
                checkbox_vip.setChecked(b? true:false);
                PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_vip",b);
            }
        });
        rl_checkbox_serial.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                boolean checked = checkbox_serial.isChecked();
                if(checked){
                    checkbox_serial.setChecked(false);
                    PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_serial",false);
                }else{
                    checkbox_serial.setChecked(true);
                    PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_serial",true);
                }
                Log.e("glk", "ipone7pluss");
            }
        });
        rl_checkbox_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                boolean checked = checkbox_all.isChecked();
                if(checked){
                    checkbox_all.setChecked(false);
                    PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_all",false);
                }else{
                    checkbox_all.setChecked(true);
                    PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_all",true);
                }
                Log.e("glk", "ipone7plusa");
            }
        });
        rl_checkbox_free.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                boolean checked = checkbox_free.isChecked();
                if(checked){
                    checkbox_free.setChecked(false);
                    PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_free",false);
                }else{
                    checkbox_free.setChecked(true);
                    PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_free",true);
                }
                Log.e("glk", "ipone7plusf");
            }
        });
        rl_checkbox_vip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                boolean checked = checkbox_vip.isChecked();
                if(checked){
                    checkbox_vip.setChecked(false);
                    PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_vip",false);
                }else{
                    checkbox_vip.setChecked(true);
                    PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_vip",true);
                }
                Log.e("glk", "ipone7plusv");
            }
        });

    }

3:动画效果

   //为popWindow添加动画效果
        mWindow.setAnimationStyle(R.style.popWindow_animation);

进出的动画style

 <style name="popWindow_animation">
        <item name="android:windowEnterAnimation">@anim/popupwindow_enter</item>
        <item name="android:windowExitAnimation">@anim/popupwindow_exit</item>
    </style>

进出的xml文件

这里写图片描述
这里写图片描述
4:我的pop 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="#fff"
      android:layout_marginLeft="@dimen/dm200"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:gravity="left"
        android:text="都市情感"
        android:padding="@dimen/dm030"
        android:textColor="@color/editTextColor"
        android:background="@drawable/selector_green_textview"
        android:textSize="@dimen/font_size_large_high"
        android:layout_height="wrap_content"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/dm001"
        android:background="#d2d2d2"/>
    <TextView
        android:layout_width="match_parent"
        android:gravity="left"
        android:text="书籍状态"
        android:padding="@dimen/dm040"
        android:textColor="@color/accountColor"
        android:background="@drawable/selector_green_textview"
        android:textSize="@dimen/font_size_large"
        android:layout_height="wrap_content"/>
<RelativeLayout
    android:id="@+id/rl_checkbox_serial"
    android:layout_width="match_parent"
    android:padding="@dimen/dm020"
    android:background="@drawable/selector_green_textview"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:text="连载"
        android:layout_marginLeft="@dimen/dm030"
        android:layout_centerInParent="true"
        android:textColor="@color/editTextColor"
        android:textSize="@dimen/font_size_middle"
        android:layout_alignParentLeft="true"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:id="@+id/checkbox_serial"
        android:layout_width="wrap_content"
        android:checked="true"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:button="@drawable/check_box_selector"
        android:layout_height="wrap_content"/>
</RelativeLayout>
    <RelativeLayout
        android:id="@+id/rl_checkbox_all"
        android:layout_width="match_parent"
        android:padding="@dimen/dm020"
        android:background="@drawable/selector_green_textview"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:text="完本"
            android:layout_marginLeft="@dimen/dm030"
            android:layout_centerInParent="true"
            android:textColor="@color/editTextColor"
            android:textSize="@dimen/font_size_middle"
            android:layout_alignParentLeft="true"
            android:layout_height="wrap_content"/>
        <CheckBox
            android:id="@+id/checkbox_all"
            android:checked="true"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerInParent="true"
            android:button="@drawable/check_box_selector"
            android:layout_height="wrap_content"/>
    </RelativeLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/dm001"
        android:background="#d2d2d2"/>
    <TextView
        android:layout_width="match_parent"
        android:gravity="left"
        android:text="收费状态"
        android:padding="@dimen/dm040"
        android:textColor="@color/accountColor"
        android:background="@drawable/selector_green_textview"
        android:textSize="@dimen/font_size_large"
        android:layout_height="wrap_content"/>
    <RelativeLayout
        android:id="@+id/rl_checkbox_free"
        android:layout_width="match_parent"
        android:padding="@dimen/dm020"
        android:background="@drawable/selector_green_textview"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:text="免费"
            android:layout_marginLeft="@dimen/dm030"
            android:layout_centerInParent="true"
            android:textColor="@color/editTextColor"
            android:textSize="@dimen/font_size_middle"
            android:layout_alignParentLeft="true"
            android:layout_height="wrap_content"/>
        <CheckBox
            android:id="@+id/checkbox_free"
            android:checked="true"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerInParent="true"
            android:button="@drawable/check_box_selector"
            android:layout_height="wrap_content"/>
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/rl_checkbox_vip"
        android:layout_width="match_parent"
        android:padding="@dimen/dm020"
        android:background="@drawable/selector_green_textview"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:text="VIP"
            android:layout_marginLeft="@dimen/dm030"
            android:layout_centerInParent="true"
            android:textColor="@color/editTextColor"
            android:textSize="@dimen/font_size_middle"
            android:layout_alignParentLeft="true"
            android:layout_height="wrap_content"/>
        <CheckBox
            android:id="@+id/checkbox_vip"
            android:checked="true"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerInParent="true"
            android:button="@drawable/check_box_selector"
            android:layout_height="wrap_content"/>

    </RelativeLayout>
<RelativeLayout
    android:layout_width="match_parent"

    android:layout_height="match_parent">
    <LinearLayout
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginBottom="@dimen/dm010"
        android:layout_height="wrap_content"
        >

        <Button
            android:id="@+id/resetting_button"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="重置"
            android:background="@color/backgroundDark"
            android:textColor="@color/colorPrimary"
            android:layout_height="match_parent"/>
        <Button
            android:id="@+id/ok_button"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="确定"
            android:background="@color/colorPrimary"
            android:layout_height="match_parent"/>
    </LinearLayout>
</RelativeLayout>
</LinearLayout>

5:要想解决点击阴影部分弹窗消失,但是又不能触发弹窗下面的recycleview条目的点击事件加上这样几行
不要设置那个外部触摸的监听,因为无论你返回true 还是false,都会出现状况,要么下方按钮点击没响应要么弹窗消失不了要么。。。。很多种都试试了,真恶心

 mWindow.setOutsideTouchable(true);
        mWindow.setTouchable(true);
       mWindow.setFocusable(true);

6:有疑问联系18792586740@163.com
福利地址点进来吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值