如何防止在窗口管理器中触摸?

时间:2018-02-07 09:59:13

标签: android

我有一个服务,我希望使用我成功实现的窗口管理器来显示布局但我需要做两件事我想在布局中访问一个按钮并防止外部触摸。我该怎么做

 final WindowManager windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
        LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        final RelativeLayout layout=(RelativeLayout) inflater.inflate(R.layout.sheet,null);
        cancel = layout.findViewById(R.id.img_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                windowManager.removeViewImmediate(layout);
            }
        });
        WindowManager.LayoutParams p = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.RGBA_8888);


        p.gravity = Gravity.BOTTOM;

        windowManager.addView(layout,p);

2 个答案:

答案 0 :(得分:1)

添加这些标志

WindowManager.LayoutParams p = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? LayoutParams.TYPE_APPLICATION_OVERLAY : LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.RGBA_8888);

答案 1 :(得分:0)

Window window = this.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

覆盖对话框的onTouchEvent()

public boolean onTouchEvent(MotionEvent event)  {  

   if(event.getAction() == MotionEvent.ACTION_OUTSIDE){  
    System.out.println("TOuch outside the dialog ******************** ");  

   }  
   return false;  
 }