OnClick / OnTouch 用户改变主意

OnClick / OnTouch user changing their mind

我目前正在试验 onclick 监听器和 on touch 监听器。

我发现即使用户将 his/her 手指从元素上拖开,应用程序也将始终执行该操作。但我希望用户能够改变主意并将 his/her 手指拖离元素,这样它就不会执行操作。我设法通过触摸做到这一点。

触摸代码:

action_bar_group_info.setOnTouchListener(new View.OnTouchListener() {
        int [] location = new int[4];
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            location[0] = action_bar_group_info.getLeft();
            location[1] = action_bar_group_info.getTop();
            location[2] = action_bar_group_info.getBottom();
            location[3] = action_bar_group_info.getRight();

            Log.v("Location: ", "top: "+String.valueOf(location[1]) + " left: "+String.valueOf(location[0]) + " right: "+String.valueOf(location[3]) + " bottom: "+String.valueOf(location[2]));
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                action_bar_group_info.setBackgroundColor(Color.parseColor("#ec9169"));
                return true;
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if ((int) event.getX() > location[0] && (int) event.getX() < location[3] && (int) event.getY() > location[1] && (int) event.getY() < location[2]) {
                    action_bar_group_info.setBackgroundColor(Color.parseColor("#ec9169"));
                    Intent intent = new Intent(mContext, GroupInformation.class);
                    intent.putExtra("gid", gid);
                    intent.putExtra("gname", gname);
                    intent.putExtra("image", img);
                    intent.putExtra("uid", uid);
                    startActivity(intent);
                    finish();
                }else {
                    action_bar_group_info.setBackgroundColor(Color.parseColor("#f54c00"));
                }
            }
            if(event.getAction() == MotionEvent.ACTION_MOVE){
                Log.v("Location pointer: x:"+ String.valueOf((int) event.getX())," Y: "+ String.valueOf((int) event.getY()));
                if ((int) event.getX() < location[0] || (int) event.getX() > location[3] || (int) event.getY() < location[1] || (int) event.getY() > location[2]) {
                    action_bar_group_info.setBackgroundColor(Color.parseColor("#f54c00"));
                }
            }

            return false;
        }
    });

但这真的是实现目标的方法吗?有没有更简单的方法来实现我想要的?

action_bar_group_info是线性布局。

I found out that using a onclick the app will always execute the action even if the user drags his/her finger off of the element.

不,这不是真的。将手指拖出即按钮区域不会触发 OnClickListener