简体   繁体   中英

OnClickListener will not detect click on rectangle?

I have two rectangles with each rectangle taking up half of the screen exactly and I am trying to detect clicks on the rectangles.

What I tried:

I tried to set an OnClickListener on both rectangles but when I click either rectangle a click will not be detected. I was very sure it wasn't detecting a click so I logged inside the listeners and that confirmed my suspicions; the listeners weren't being entered and therefore no click was being detected.

Here is where I declare the listeners in my activity:

topRectangle = new Rectangle(this, 0, 0, mScrWidth, mScrHeight/2, 0xC757FF57);
    bottomRectangle = new Rectangle(this,0, mScrHeight /2, mScrWidth, mScrHeight, 0xDEFF5845 );

    mainView.addView(mLine);
    mLine.invalidate();

    mainView.addView(topRectangle);
    topRectangle.invalidate();

    mainView.addView(bottomRectangle);
    bottomRectangle.invalidate();

    View.OnClickListener topListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (topRectangle.getColor() == 0xC757FF57 /*Green*/) {

                if (!isGameStarted) {
                    isGameStarted = true;
                }

                topRectangle.setColor(0xDEFF5845);

                if (movingUp) {
                    movingUp = false;
                } else {
                    movingUp = true;
                }
            }
            else if (topRectangle.getColor() == 0xDEFF5845 /*Red*/) {
                /*Player Dies*/
            }
        }
    };

    View.OnClickListener bottomListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (bottomRectangle.getColor() == 0xC757FF57 /*Green*/) {
                bottomRectangle.setColor(0xDEFF5845);

                if (movingUp) {
                    movingUp = false;
                } else {
                    movingUp = true;
                }
            }
            else if (bottomRectangle.getColor() == 0xDEFF5845 /*Red*/) {
                /*Player Dies*/
            }
        }
    };

    topRectangle.setOnClickListener(topListener);

    bottomRectangle.setOnClickListener(bottomListener);

    topRectangle.setClickable(true);
    bottomRectangle.setClickable(true);

Here is the class I have for rectangles:

public class Rectangle extends View {

public float left;
public float top;
public float right;
public float bottom;
public int color;
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

public static int[] colorArray = {0xFF949CFF, 0xffeef16b, 0xFFFFAD85, 0xFFAFFF78, 0xFFE3ABFF,
        0xFF7DFFE0, 0xFFFEBC71, 0xFFA877FB, 0xFF62FF8B, 0xFFF99AA1, 0xFFA9FF53,
        0xFFD02A21, 0xFF1D1AD0, 0xFFCED07E, 0xFF60B4FF, 0xFFFFA1E0};


/*Faded Blue, yellow, salmon, green, pink-red, light blue, light orange, purple, teal, pink,
light-green, red, blue, sand, lighter blue, pink*/
public static Random randomGenerator = new Random();


public int getColor() {
    return color;
}

public void setColor(int color) {
    mPaint.setColor(color);
    this.color = color;
}


public int getTheBottom() {
    return (int) bottom;
}


public int getTheLeft() {
    return (int) left;
}


public int getTheTop() {
    return (int) top;
}


public int getTheRight() {
    return (int) right;
}

//construct new rectangle object
public Rectangle(Context context, float left, float top, float right, float bottom, int color) {
    super(context);
    //color hex is [transparncy][red][green][blue]
    mPaint.setColor(color);  //not transparent. color is white
    this.left = left;
    this.top = top;
    this.right = right;
    this.bottom = bottom;
}

//construct new rectangle object
public Rectangle(Context context, float left, float top, float right, float bottom) {
    super(context);
    this.left = left;
    this.top = top;
    this.right = right;
    this.bottom = bottom;
}

//qcalled by invalidate()
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawRect(left, top, right, bottom, mPaint);
}

public void setX(float left, float right) {
    this.left = left;
    this.right = right;
}

public void setY(float top, float bottom) {
    this.top = top;
    this.bottom = bottom;
}

public int getRectWidth() {
    return (int) (right - left);
}

public int getRectHeight() {
    return (int) (bottom - top);
}

public int getCenterX() {
    return (int) (right + left) / 2;
}

public int getCenterY() {
    return (int) (top + bottom) / 2;
}

}

What I Have Referenced:

OnClickListener won't work

Android: onClickListener does not work as programmed

Question:

Why is no click being detected on the rectangle views?

You have told the view what to do when the user clicks on it but it looks like the view is not currently clickable. Try this:

topRectangle = new Rectangle(this, 0, 0, mScrWidth, mScrHeight/2, 0xC757FF57);
topRectangle.setClickable(true);

bottomRectangle = new Rectangle(this,0, mScrHeight /2, mScrWidth, mScrHeight, 0xDEFF5845 );
bottomRectangle.setClickable(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM