Android的4.x的 - 系统覆盖 - 无法捕捉触摸事件 [英] Android 4.x - System Overlay - Cannot capture touch events

查看:107
本文介绍了Android的4.x的 - 系统覆盖 - 无法捕捉触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的叠加应用为Android 4.4。

I am trying to create a simple overlay app for Android 4.4.

我发现一个例子在屏幕上画一个按钮,所有工作正常,但触摸事件监听器不点火。

I have found an example to draw a button over the screen, all works fine but the touch event listener is not firing.

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class HUD extends Service {
    Button mButton;

    @Override
    public IBinder
    onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //mView = new HUDView(this);
        mButton = new Button(this);
        mButton.setText("My Overlay Button");
        mButton.setClickable(true);
        mButton.setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View arg0, MotionEvent arg1) {
            mButton.setText("CLICKED!!!");
            return true;
          }
        }); 

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.RIGHT | Gravity.CENTER;
        params.setTitle("Load Average");
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.addView(mButton, params);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mButton != null)
        {
            ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mButton);
            mButton = null;
        }
    }
}

我究竟做错了什么?

What am i doing wrong?

推荐答案

使用 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT 来使视图点击4.3。

Use WindowManager.LayoutParams.TYPE_SYSTEM_ALERT to make the view clickable in 4.3.

TYPE_SYSTEM_OVERLAY 不再允许点击。

这篇关于Android的4.x的 - 系统覆盖 - 无法捕捉触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆