gpt4 book ai didi

java - 检测到按压,然后检测到 View 之外的移动

转载 作者:行者123 更新时间:2023-12-02 04:58:06 26 4
gpt4 key购买 nike

我在relativelayout上有一个监听器:

 rl.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
rl.setBackgroundColor(Color.BLACK);
}

if ((event.getAction() == MotionEvent.ACTION_UP)) {
rl.setBackgroundColor(Color.WHITE);
v.performClick();
}
return false;
}
});

当我在布局上按下/向上时,颜色会改变。我想检测的是当用户按下屏幕,但将手指移离 View (但仍然按下)时。我不知道该怎么做。谢谢。

最佳答案

给你。应该使背景向下为黑色,向上为白色。如果手指移出视野,它将返回白色。

rl.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
if (isTouchInView(view, motionEvent)) {
//lifted finger while touch was in view
view.performClick();
}

view.setBackgroundColor(Color.WHITE);
return true;
}

if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
//finger still down and left view
if (!isTouchInView(view, motionEvent)) {
view.setBackgroundColor(Color.WHITE);
}
}

if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
//pressed down on view
view.setBackgroundColor(Color.BLACK);
return true;
}

if (motionEvent.getAction() == MotionEvent.ACTION_CANCEL) {
//a cancel event was received, finger up out of view
view.setBackgroundColor(Color.WHITE);
return true;
}
return false;
}

private boolean isTouchInView(View view, MotionEvent event) {
Rect hitBox = new Rect();
view.getGlobalVisibleRect(hitBox);
return hitBox.contains((int) event.getRawX(), (int) event.getRawY());
}

关于java - 检测到按压,然后检测到 View 之外的移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28571073/

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