簡體   English   中英

當有人觸摸我的應用上的紅色圓圈時,什么也沒有發生

[英]When someone touches a red circle on my app nothing happens

我正在制作一個在屏幕上隨機顯示圓圈的應用程序。 圓圈是紅色或綠色。 該應用程序的目的是,當有人觸摸綠色圓圈時,會發生一些不錯的事情,就像他們將分數加到分數上一樣。 單擊紅色圓圈時,會發生不良情況,例如啟動新活動,並且頁面上顯示您失敗或其他原因。 這是我為此應用編寫的代碼。 在這段代碼中,我沒有收到任何錯誤,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開發人員參考-View.OnTouchListener

無論哪種方式,使視圖實現此接口以偵聽自身都感覺不對。 相反,您可能需要看一下View.onTouchEvent(MotionEvent event)方法。 也許它符合您的目的(或者在這種情況下應該如此)。 偵聽器接口應該由外部組件實現。 舉例來說,如果您想讓TextView每次觸摸Button或ImageView時都進行監聽,則可以擴展TextView / Button並使它們實現Listener接口,然后將其作為setOnTouchListener(View.OnTouchListener)的參數)傳遞給您的視圖setOnTouchListener(View.OnTouchListener)

但是,所有視圖都有一個名為onTouchEvent()的方法。 如果您想視圖本身中偵聽事件則應使用此View.onTouchEvent()方法,因為默認情況下,只要觸摸到該視圖,就會調用該方法。 如果您需要視圖本身這種方法的引用,你可以調用this自認為自己將是你目前的范圍。

Android開發人員參考-View.onTouchEvent(MotionEvent事件)

如果你不喜歡這樣,你需要為了做使你的代碼的工作就是改變你的onTouch()方法的重寫onTouchEvent()是這樣的:(觸摸動作還增加了驗證,通過普加和湯姆·建議因此您無需考慮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一起使用,並且通常在自定義視圖類之外定義。 例如:

    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;
}

有關更多詳細信息,請參見以下內容: 輸入事件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM