gpt4 book ai didi

android - 无法获取 TYPE_SYSTEM_OVERLAY 的触摸事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:49 28 4
gpt4 key购买 nike

我喜欢开发类似于 Swapps 的应用程序.我正在尝试在任何屏幕顶部显示一个按钮,并且应该只在滑动操作时出现。我尝试使用以下代码

    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);

当用户从左向右滑动时,我试图通过捕获该事件并执行

    parentlay.addView(bt);

并在用户从右向左滑动时移除 View

    parentlay.removeView(bt);

如前所述,除非我将类型更改为 TYPE_SYSTEM_ALERT ,否则我无法获得运动事件,但这反过来会卡住所有屏幕并且无法单击返回或主页按钮,是否有任何替代方法可以做到这一点。

我刚试过下面的修改

    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 则我无法获得点击事件。

最佳答案

要隐藏 View ,您需要调用 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);
}
});

}

关于android - 无法获取 TYPE_SYSTEM_OVERLAY 的触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14938853/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com