Android-如何使屏幕叠加层上的按钮可点击

时间:2018-10-29 10:35:40

标签: java android

我正在尝试通过屏幕覆盖实现一个简单的锁屏。我在XML布局中添加了一个按钮,该应用程序成功覆盖了屏幕,但该按钮似乎不起作用。

    public WindowManager winManager;
    public RelativeLayout wrapperView;

    @Override
    protected void onCreate(Bundle savedInstanceState). {      ((KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock("IN").disableKeyguard();
    PackageManager p = getPackageManager();
            p.setComponentEnabledSetting(getComponentName(),     PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
        finish();
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams     localLayoutParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|

                                                                                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
                                                                                      WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                                                                                      PixelFormat.TRANSLUCENT);

        this.winManager =     ((WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE));
        this.wrapperView = new     RelativeLayout(getBaseContext());
        getWindow().setAttributes(localLayoutParams);
        View.inflate(this, R.layout.lackiscreen, this.wrapperView);
        this.winManager.addView(this.wrapperView, localLayoutParams);


    }
    public void dclick(View view){
        this.winManager.removeView(this.wrapperView);
        this.wrapperView.removeAllViews();
    }

1 个答案:

答案 0 :(得分:0)

Android仅允许非常特定的窗口在覆盖时接收触摸事件。您需要正确设置标志。

WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
);

希望有帮助。 (至少对我有用)