当有人触摸我应用程序上的红色圆圈时,没有任何反应

When someone touches a red circle on my app nothing happens

我正在制作一个在屏幕上随机显示圆圈的应用程序。圆圈是红色或绿色的。该应用程序的目的是当有人触摸绿色圆圈时会发生一些好事,比如他们的分数会增加。单击红色圆圈时,会发生一些不好的事情,比如启动了一个新的 activity 并且页面显示您失败了或其他什么。这是我为这个应用程序编写的代码。在此代码中,我没有收到任何错误,logcat 中没有任何错误,一切正常。圆圈随机显示在屏幕上,分数默认为 0。我在使用此应用程序时遇到的问题是,当单击红色或绿色圆圈时,没有任何反应。

public DrawingView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

    }
    RectF rectf = new RectF(0, 0, 200, 0);

    private static final int w = 100;
    public static int lastColor = Color.BLACK;
    private final Random random = new Random();
    private final Paint paint = new Paint();
    private final int radius = 230;
    private final Handler handler = new Handler();
    public static int redColor = Color.RED;
    public static int greenColor = Color.GREEN;
    int randomWidth = 0;
    int randomHeight = 0;
    public static int addPoints = 0;


    private final Runnable updateCircle = new Runnable() {
        @Override 
        public void run() {
            lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
            paint.setColor(lastColor);
            invalidate();
            handler.postDelayed(this, 1000);

        }
    };

    @Override 
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        handler.post(updateCircle);
    }

    @Override 
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        handler.removeCallbacks(updateCircle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // your other stuff here
        if(random == null){
            randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
            randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
        }else {
            randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
            randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
        }

        canvas.drawCircle(randomWidth, randomHeight, radius, paint);

        paint.setColor(Color.BLACK);
        paint.setTextSize(150);
        canvas.drawText("Score: " + addPoints, 120, 300, paint);
    }

    public boolean onTouch(View v, MotionEvent event) {
   int x = (int) event.getX();
   int y = (int) event.getY();
   if(isInsideCircle(x, y) ==  true){
      //Do your things here
       if(redColor == lastColor){
          Intent i = new Intent(v.getContext(), YouFailed.class);
          v.getContext().startActivity(i);
       } else {
           addPoints++;
       }
   }else {

   }
   return true;
}

public boolean isInsideCircle(int x, int y){
  if ((((x - randomWidth)*(x - randomWidth)) + ((y - randomHeight)*(y - randomHeight))) < ((radius)*(radius))){
    return true;
  }
  return false; 
}


}
  1. 您的视图可能没有实现 View.OnTouchListener 接口,因此它没有调用 onTouch() 方法。

  2. 您的视图未通过视图的 View.setOnTouchListener(View.OnTouchListener) 方法设置为 View.OnTouchListener。

Android Developer Reference - View.OnTouchListener

无论哪种方式,让视图实现此接口来监听自身都是错误的。相反,您可能想看看 View.onTouchEvent(MotionEvent event) 方法;也许它符合您的目的(或者在这种情况下应该)。侦听器接口应该由外部组件实现。比方说,如果你想让你的 TextView 在每次触摸 Button 或 ImageView 时监听,你可以扩展 TextView/Button 并让它们实现 Listener 接口,然后将它作为它的参数传递给你查看 setOnTouchListener(View.OnTouchListener).

但是,所有视图都有一个名为 onTouchEvent() 的方法。如果您想在 视图本身中监听 事件,则应使用此 View.onTouchEvent() 方法,因为默认情况下,每当视图被触动时都会调用它,很好 touched 。如果您需要使用这种方法引用视图本身,您可以调用 this 因为视图本身就是您当前的范围。

Android Developer Reference - View.onTouchEvent(MotionEvent event)

如果您这样做,为了使您的代码正常工作,您需要做的就是将您的 onTouch() 方法更改为 onTouchEvent() 的重写,如下所示:(还添加了验证Pooja 和 Tom 建议的触摸操作,因此您无需考虑 MOVEUP 事件。根据您想要触发的时间,将 DOWN 更改为 UP事件)

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        if(isInsideCircle(x, y) ==  true) {
            //Do your things here
            if(redColor == lastColor){
                Intent i = new Intent(v.getContext(), YouFailed.class);
                this.getContext().startActivity(i);
            } else {
                addPoints++;
            }
        } else {
             //Handle case not inside the circle
        }
    }

    return true;
}

onTouch 方法旨在与 OnTouchListener 一起使用,并且通常在您的自定义视图 class 之外定义。例如:

    this.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // touch handling code
            return true;
        }
    });

如果您要在自定义视图中查找触摸事件,则应实现 onTouchEvent 方法。您可能还需要检查 ACTION_UPACTION_DOWN,否则您将处理多个触摸事件。

@Override
public boolean onTouchEvent(MotionEvent event) {

    boolean result = false;

    if (event.getAction() == MotionEvent.ACTION_DOWN) {

        int x = (int) event.getX();
        int y = (int) event.getY();

        if(isInsideCircle(x, y) ==  true) {
            //Do your things here
            if(redColor == lastColor){
               Intent i = new Intent(v.getContext(), YouFailed.class);
               v.getContext().startActivity(i);
            } else {
               addPoints++;
            }
            result = true;
        }
    }

    return result;
}

有关详细信息,请参阅以下内容: Input Events