Java 未调用onTouchEvent

Java 未调用onTouchEvent,java,android,ontouchlistener,touch-event,android-event,Java,Android,Ontouchlistener,Touch Event,Android Event,我正在制作一个游戏应用程序,在屏幕上显示随机圆圈。这都是在onDraw方法中通过创建一个永无止境的循环来完成的。然而,在onTouchEvent方法中,有一些代码在单击圆时被调用。问题是当一个圆被“触摸”时,什么也不会发生,但有时会发生(如果你在它消失之前多次点击它)。我想知道是否有一种方法可以让onTouch方法工作,这样就可以点击这些圆圈 public class DrawingView extends View{ public DrawingView(Context cont

我正在制作一个游戏应用程序,在屏幕上显示随机圆圈。这都是在onDraw方法中通过创建一个永无止境的循环来完成的。然而,在onTouchEvent方法中,有一些代码在单击圆时被调用。问题是当一个圆被“触摸”时,什么也不会发生,但有时会发生(如果你在它消失之前多次点击它)。我想知道是否有一种方法可以让onTouch方法工作,这样就可以点击这些圆圈

public class DrawingView extends View{



    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;
    public static int savedScore;
    public static List<String> a = new ArrayList<String>();
    public static String[] savedScores = new String[a.size()];
    Paint red;
    public static int howManyPoints;
    public static int highestScore = 0;
    boolean isTouched;
    Thread newThread = new Thread();
    int t = 1;
    int l = 0;


    @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
        Paint back = new Paint();
        back.setColor(Color.BLACK);
        Rect background = new Rect();
        background.set(0, 0, canvas.getWidth(),canvas.getHeight() );
        canvas.drawRect(background, back);

        Paint newPaint = new Paint();
        newPaint.setColor(Color.BLUE);
        newPaint.setTextSize(60);
        canvas.drawText("Beta v2", 10, 60, newPaint);

        if(l < t){
            lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
            paint.setColor(lastColor);
            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);
            try {
                newThread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            invalidate();
        }


        red = new Paint();
        red.setColor(Color.BLUE);
        red.setTextSize(150);
        canvas.drawText("" + addPoints, 500, 1350, red);


    }




    @SuppressWarnings("deprecation")
    @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){
                        if(lastColor == redColor){
                            howManyPoints = addPoints;
                            if(howManyPoints > highestScore){
                                highestScore = howManyPoints;
                            }
                            //handler.removeCallbacks(updateCircle);
                            lastColor = redColor;
                            addPoints = 0;
                            Intent i = new Intent(this.getContext(), YouFailed.class);
                            this.getContext().startActivity(i);
                            l = 1;
                        } 
                        if(lastColor == greenColor){
                            addPoints++;
                            isTouched = true;
                            l = 0;

                        }
                }else  {

                }

        }
        return false;

}


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

}
公共类DrawingView扩展视图{
公共绘图视图(上下文){
超级(上下文);
//TODO自动生成的构造函数存根
}
RectF RectF=新的RectF(0,0,200,0);
专用静态最终int w=100;
公共静态int lastColor=Color.BLACK;
私有最终随机=新随机();
私人最终油漆=新油漆();
专用最终整数半径=230;
私有最终处理程序=新处理程序();
公共静态int redColor=Color.RED;
公共静态int greenColor=Color.GREEN;
int-randomWidth=0;
int-randomHeight=0;
公共静态int addPoints=0;
公共静态int-savedScore;
public static List a=new ArrayList();
公共静态字符串[]savedScores=新字符串[a.size()];
涂红色;
公共静态int多少点;
公共静态int最高分=0;
布尔值是非接触的;
线程newThread=新线程();
int t=1;
int l=0;
@凌驾
受保护的无效数据附加到DOWINDOW(){
super.onAttachedToWindow();
//handler.post(更新循环);
}
@凌驾
受保护的无效onDetachedFromWindow(){
super.onDetachedFromWindow();
//handler.removeCallbacks(updateCircle);
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
//你的其他东西在这里
油漆背面=新油漆();
back.setColor(Color.BLACK);
Rect background=new Rect();
set(0,0,canvas.getWidth(),canvas.getHeight());
canvas.drawRect(背景,背面);
油漆新油漆=新油漆();
newPaint.setColor(颜色:蓝色);
newPaint.setTextSize(60);
画布.drawText(“Beta v2”,10,60,newPaint);
if(l最高分数){
最高分数=多少分;
}
//handler.removeCallbacks(updateCircle);
lastColor=redColor;
addPoints=0;
Intent i=新Intent(this.getContext(),YouFailed.class);
this.getContext().startActivity(i);
l=1;
} 
if(lastColor==绿色){
addPoints++;
isTouched=true;
l=0;
}
}否则{
}
}
返回false;
}
公共布尔IsInsidercle(整数x,整数y){
如果(((x-随机宽度)*(x-随机宽度))+((y-随机高度)*(y-随机高度))<((半径)*(半径))){
返回true;
}
返回false;
}
}

您在触地时忘记返回true

试着加上这个

if(event.getAction() == MotionEvent.ACTION_DOWN){
    //your code
    return true;
    }

请重新格式化你的代码,它很难读懂,所以我在return false上面加了一个return true?你在onTouchEvent方法中返回false,但如果(event.getAction()==MotionEvent.ACTION_DOWN){}那不是你代码中的问题,我看到你每秒都会产生一个新的循环,所以我假设屏幕上会有多个圆圈。在isInsideCircle中,randomWidth和randomHeight的值是最近一个圆的位置,但您正在触摸的圆可能不是最新的圆。也许这就是问题的原因。多个圆圈不会同时出现在屏幕上。它每秒钟都会重画一次