无法获得TYPE_SYSTEM_OVERLAY触摸事件 [英] Could not get Touch event for TYPE_SYSTEM_OVERLAY

查看:2162
本文介绍了无法获得TYPE_SYSTEM_OVERLAY触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个应用程序,类似于 Swapps .I想显示在任何屏幕顶部的按钮,并只在刷卡action.I试着用下面的code应该发生

I like to develop an app that is similar to Swapps .i am trying to display a button on top of any screen and that should occur only on a swiping action.I tried with the below code

    WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    params.type =WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
    params.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
    params.format =PixelFormat.TRANSLUCENT; 
    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
    params.width = WindowManager.LayoutParams.WRAP_CONTENT;
    parentlay = new LinearLayout(this);
    mybt = new Button(getApplicationContext());
    bt.setText("Click to Go");

    parentlay.setBackgroundColor(Color.TRANSPARENT);
    parentlay.setHapticFeedbackEnabled(true);
    parentlay.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
                // event.getAction() prints 4 and not able to get up or down event
           }
       });
   windowmanager.addView(parentlay, params);

我想,当用户从挥笔显示按钮通过捕获该事件,做左到右

I am trying to display the button when user swipes from left-to-right by capturing that event and doing

    parentlay.addView(bt);

和当用户从右到左刷卡通过

and remove the view when user swipes from right-to-left by

    parentlay.removeView(bt);

正如前面提到的我coudn't获得移动事件,除非我改变了类型TYPE_SYSTEM_ALERT,但inturn冻结所有画面,无法单击后退或home键,则有做这样的替代方法。

As mentioned i coudn't get the motion events unless i changed the type as TYPE_SYSTEM_ALERT ,but that inturn freezes all the screen and could not click back or home button,is there is any alternate way to do this.

我刚刚尝试下面的修改

    params.type =WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
    |WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
   |WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

    params.height = WindowManager.LayoutParams.MATCH_PARENT;
    params.width = WindowManager.LayoutParams.MATCH_PARENT;

但我不能点击下方按钮,可能是其因MATCH_PARTENT,但如果我将它设置为wrap_content然后我不能让单击事件。

But i couldn't click the underneath button ,may be its due to MATCH_PARTENT but if i set it to wrap_content then i cannot get click events.

推荐答案

要隐藏你需要调用mWindowManager.removeView(mOverlay)的观点;

To hide the view you need to call mWindowManager.removeView(mOverlay);

private RelativeLayout mOverlay;
        private WindowManager mWindowManager;   
     WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                                | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                        PixelFormat.TRANSLUCENT);
                params.gravity = Gravity.TOP;
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                mOverlay = (RelativeLayout) inflater
                        .inflate(R.layout.phasechange, null);

                mWindowManager = (WindowManager) context
                        .getSystemService(Context.WINDOW_SERVICE);
                mWindowManager.addView(mOverlay, params);
                Button b = (Button) mOverlay.findViewById(R.id.button1);
                b.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        mWindowManager.removeView(mOverlay);
                    }
                });

            }

这篇关于无法获得TYPE_SYSTEM_OVERLAY触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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