浮窗WindowManager addView响应返回按键

本文详细介绍了如何在Android应用中正确创建并显示一个可以接收Back按键事件的悬浮窗口。重点讲解了避免使用WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE标志,以确保悬浮窗能够捕获到Back按键事件,并给出了具体的代码实现。

显示悬浮框,注意一定不要设置WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,拿不到Focus是接收不到back事件的。

private void showPreviewDialog(){
    View mPreviewLayout = LayoutInflater.from(getContext()).inflate(R.layout.window_preview,null);
    WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_APPLICATION_PANEL,
            /*WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE*/0,
            PixelFormat.TRANSLUCENT);
    mPreviewLayout.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            Logger.d(TAG,"onKey#keyCode="+keyCode+",event.getKeyCode()="+event.getKeyCode()+",event.getAction()="+event.getAction());
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
               //do sth.
            }
            return false;
        }
    });
    mWindowManager.addView(mPreviewLayout,layoutParams);
}

window_preview.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<com.sunny.widget.BwRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <!--省略其它代码 -->

</com.sunny.widget.BwRelativeLayout>

BwRelativeLayout:

package com.sunny.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.RelativeLayout;

public class BwRelativeLayout extends RelativeLayout {
    public BwRelativeLayout(Context context) {
        super(context);
    }

    public BwRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BwRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
            if(mOnKeyListener != null){
                return mOnKeyListener.onKey(this,event.getKeyCode(),event);
            }
        }
        return super.dispatchKeyEvent(event);
    }

    OnKeyListener mOnKeyListener = null;

    @Override
    public void setOnKeyListener(OnKeyListener onKeyListener) {
        this.mOnKeyListener = onKeyListener;
        super.setOnKeyListener(onKeyListener);
    }
}
package com.example.myapplicationdemo4.listView10; import android.content.Context; import android.graphics.PixelFormat; import android.os.Build; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.widget.TextView; import com.example.myapplicationdemo4.R; public class FloatingWindowManager { private Context context; private WindowManager windowManager; private View floatingView; // 关键:保存悬浮窗View的引用 private WindowManager.LayoutParams params; public FloatingWindowManager(Context context) { this.context = context.getApplicationContext(); windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); } // 创建悬浮窗 public void createFloatingWindow() { WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, PixelFormat.TRANSLUCENT); params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL; params.x = 0; params.y = 0; // 关键:Inflate布局并保存引用 floatingView = LayoutInflater.from(context).inflate(R.layout.floating_layout, null); // 初始内容 TextView tvContent = floatingView.findViewById(R.id.tvContent); tvContent.setText("初始内容"); windowManager.addView(floatingView, params); } // 更新悬浮窗内容(核心方法) public void updateContent(String newText) { if (floatingView == null) return; // 通过保存的View引用直接修改内容 TextView tvContent = floatingView.findViewById(R.id.tvContent); tvContent.setText(newText); } // 移除悬浮窗 public void removeFloatingWindow() { if (floatingView != null) { windowManager.removeView(floatingView); floatingView = null; } } } 请帮我修改一下这个代码,来实现这个悬浮窗点击穿透到悬浮窗下层的界面
07-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值