【问题标题】:MotionEvent and the touch detector?MotionEvent 和触摸检测器?
【发布时间】:2018-06-24 04:44:11
【问题描述】:

我想问一个问题:

我使用 MotionEvent 代码拖动我正在开发的问答游戏的按钮,问题是在我开始拖动它后我无法再单击拖动的按钮,它看起来像它停留总是在 ACTION_DOWN,我也尝试了 CANCEL 和 UP 动作,但还是一样!

此外,当拖动的按钮 [btn1] 触摸另一个按钮 [btn2] 时,我如何让拖动的按钮在触摸另一个按钮或项目时执行某些操作(就像我希望它检测到其他项目的触摸一样)例如改变颜色!!

3 个问题:

  • 起不来

  • 你不能再点击它了

  • 触摸检测

任何建议如何解决这个问题!并提前 ty

import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.content.*;
import android.graphics.*;
import android.media.*;
import android.net.*;
import android.text.*;
import android.util.*;
import android.webkit.*;
import android.animation.*;
import android.view.animation.*;
import java.util.*;
import java.text.*;
import android.app.Activity;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Button;
import java.util.Timer;
import java.util.TimerTask;
import android.view.View;


    public class MainActivity extends Activity {

        private Timer _timer = new Timer();

        private double res = 0;
        private double lvl = 0;
        private String message = "";

        private LinearLayout linear3;
        private TextView textview13;
        private LinearLayout linear11;
        private LinearLayout linear13;
        private LinearLayout linear12;
        private LinearLayout linear8;
        private Button button1;
        private TextView textview14;
        private Button button4;
        private Button button2;
        private Button button3;

        private TimerTask m;
        @Override
        protected void onCreate(Bundle _savedInstanceState) {
            super.onCreate(_savedInstanceState);
            setContentView(R.layout.main);
            initialize();
            initializeLogic();
        }

        private void initialize() {

            linear3 = (LinearLayout) findViewById(R.id.linear3);
            textview13 = (TextView) findViewById(R.id.textview13);
            linear11 = (LinearLayout) findViewById(R.id.linear11);
            linear13 = (LinearLayout) findViewById(R.id.linear13);
            linear12 = (LinearLayout) findViewById(R.id.linear12);
            linear8 = (LinearLayout) findViewById(R.id.linear8);
            button1 = (Button) findViewById(R.id.button1);
            textview14 = (TextView) findViewById(R.id.textview14);
            button4 = (Button) findViewById(R.id.button4);
            button2 = (Button) findViewById(R.id.button2);
            button3 = (Button) findViewById(R.id.button3);

            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    button1.setOnTouchListener(new OnTouchListener() {
                        PointF DownPT = new PointF();
                        PointF StartPT = new PointF();
                        @Override public boolean onTouch(View v, MotionEvent event) {
                            int eid = event.getAction();
                            switch (eid) {
                                case MotionEvent.ACTION_MOVE:PointF mv = new PointF(event.getX() - DownPT.x, event.getY() - DownPT.y);
                                button1.setX((int)(StartPT.x+mv.x));
                                button1.setY((int)(StartPT.y+mv.y));
                                StartPT = new PointF(button1.getX(), button1.getY());
                                break;
                                case MotionEvent.ACTION_DOWN : DownPT.x = event.getX();
                                DownPT.y = event.getY();
                                StartPT = new PointF(button1.getX(), button1.getY());
                                break;
                                case MotionEvent.ACTION_UP :
                                break;
                                default : break;
                            }
                            return true;
                        }
                    });
                    textview13.setText(message);
                }
            });

            button4.setOnClickListener(new View.OnClickListener() {
                @Override
public void onClick(View view) {

            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                button1.setBackgroundColor(0xFFCFD8DC);
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }
    private void initializeLogic() {
        /**
android.graphics.drawable.GradientDrawable gd = new android.graphics.drawable.GradientDrawable(); 
gd.setCornerRadius(30);**/
         /* radius */
        /*gd.setStroke(4, Color.WHITE);*/
         /* stroke heigth and color */

        m = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        lvl = 1;
                        textview14.setText("من هو مستر صلصا ؟");
                        button2.setText("مدير قناة صلصا");
                        button3.setText("صاحب سلسلة \nألف ليلة و ليلة");
                        button4.setText("لا أعلم");
                    }
                });
            }
        };
        _timer.schedule(m, (int)(1000));
        m = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if ((((SketchwareUtil.getLocationX(button1) + 75) > (SketchwareUtil.getLocationX(button2) - 120)) && ((SketchwareUtil.getLocationX(button1) - 125) < (SketchwareUtil.getLocationX(button2) + 120))) && (((SketchwareUtil.getLocationY(button1) + 130) > (SketchwareUtil.getLocationY(button2) - 60)) && ((SketchwareUtil.getLocationY(button1) - 70) < (SketchwareUtil.getLocationY(button2) + 60)))) {
                            res = 1;
                            button2.setBackgroundColor(0xFF64DD17);
                        }
                        if ((((SketchwareUtil.getLocationX(button1) + 75) > (SketchwareUtil.getLocationX(button3) - 120)) && ((SketchwareUtil.getLocationX(button1) - 125) < (SketchwareUtil.getLocationX(button3) + 120))) && (((SketchwareUtil.getLocationY(button1) + 130) > (SketchwareUtil.getLocationY(button3) - 60)) && ((SketchwareUtil.getLocationY(button1) - 70) < (SketchwareUtil.getLocationY(button3) + 60)))) {
                            res = 2;
                            button3.setBackgroundColor(0xFFF44336);
                        }
                        if ((((SketchwareUtil.getLocationX(button1) + 75) > (SketchwareUtil.getLocationX(button4) - 120)) && ((SketchwareUtil.getLocationX(button1) - 125) < (SketchwareUtil.getLocationX(button4) + 120))) && (((SketchwareUtil.getLocationY(button1) + 130) > (SketchwareUtil.getLocationY(button4) - 60)) && ((SketchwareUtil.getLocationY(button1) - 70) < (SketchwareUtil.getLocationY(button4) + 60)))) {
                            res = 3;
                            button4.setBackgroundColor(0xFFF44336);
                        }
                    }
                });
            }
        };
        _timer.scheduleAtFixedRate(m, (int)(1000), (int)(100));
    }

    @Override
    protected void onActivityResult(int _requestCode, int _resultCode, Intent _data) {
        super.onActivityResult(_requestCode, _resultCode, _data);

        switch (_requestCode) {

            default:
            break;
        }
    }

【问题讨论】:

  • 您可能想粘贴一个代码示例,并在您尝试过的问题中更详细地介绍一下。
  • 哦,好的,我会放代码
  • 对。您还需要使用正确的大写字母和标点符号。我们不是lolcats。我应该是我,你应该是你。逗号或感叹号前也不需要空格。
  • 好的,对不起,我是新来的,请教你的建议
  • 你为你的按钮设置了 onClickListener 吗?

标签: java android object-detection motionevent


【解决方案1】:

改一下

button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    button1.setOnTouchListener(new OnTouchListener() {
                        PointF DownPT = new PointF();
                        PointF StartPT = new PointF();
                        @Override public boolean onTouch(View v, MotionEvent event) {
                            int eid = event.getAction();
                            switch (eid) {
                                case MotionEvent.ACTION_MOVE:PointF mv = new PointF(event.getX() - DownPT.x, event.getY() - DownPT.y);
                                button1.setX((int)(StartPT.x+mv.x));
                                button1.setY((int)(StartPT.y+mv.y));
                                StartPT = new PointF(button1.getX(), button1.getY());
                                break;
                                case MotionEvent.ACTION_DOWN : DownPT.x = event.getX();
                                DownPT.y = event.getY();
                                StartPT = new PointF(button1.getX(), button1.getY());
                                break;
                                case MotionEvent.ACTION_UP :
                                break;
                                default : break;
                            }
                            return true;
                        }
                    });
                    textview13.setText(message);
                }
            });

到这里

int lastAction;

    button1.setOnTouchListener(new OnTouchListener() {
                            PointF DownPT = new PointF();
                            PointF StartPT = new PointF();
                            @Override public boolean onTouch(View v, MotionEvent event) {

                                int eid = event.getAction();
                                switch (eid) {
                                    case MotionEvent.ACTION_MOVE:PointF mv = new PointF(event.getX() - DownPT.x, event.getY() - DownPT.y);
                                    button1.setX((int)(StartPT.x+mv.x));
                                    button1.setY((int)(StartPT.y+mv.y));
                                    StartPT = new PointF(button1.getX(), button1.getY());
                                    lastAction = MotionEvent.ACTION_MOVE;
                                    break;
                                    case MotionEvent.ACTION_DOWN : DownPT.x = event.getX();
                                    DownPT.y = event.getY();
                                    StartPT = new PointF(button1.getX(), button1.getY());
                                    lastAction = MotionEvent.ACTION_DOWN;
                                    break;
                                    case MotionEvent.ACTION_UP :
                                    if(lastAction == MotionEvent.ACTION_DOWN){
                                      textview13.setText(message);
                                     }
                                    lastAction = MotionEvent.ACTION_UP;
                                    break;
                                    default : break;
                                }
                                return true;
                            }
                        });

对于第二部分,您需要拥有其他项目的坐标,然后检查您的 event.getY()event.getX() 是否在其他项目的坐标范围内。

【讨论】:

  • 我觉得没什么改变,问题还是一样
  • @amiine 我已经更新了我的答案,检查新代码,看看它是否工作正常。我已经删除了 onClickListener() 。
  • lastAction=MotionEvent.ACTION_MOVE;
  • 错误(不能引用封闭范围内定义的非最终局部变量lastAction)
  • IF 和 ACTION_DOWN/UP TOO 的错误相同
【解决方案2】:

哇!我不小心找到了解决方案

所有问题!

我只是改变了:

return true;return false;

我只是想看看差异!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多