Java OnClickListener不会检测到单击矩形?

Java OnClickListener不会检测到单击矩形?,java,android,view,onclicklistener,rectangles,Java,Android,View,Onclicklistener,Rectangles,我有两个矩形,每个矩形正好占据屏幕的一半,我试图检测矩形上的点击 我的尝试: 我试图在两个矩形上都设置OnClickListener,但当我单击任一矩形时,都不会检测到单击。我非常确定它没有检测到点击,所以我登录到了监听器中,这证实了我的怀疑;未输入侦听器,因此未检测到任何单击 下面是我在活动中声明侦听器的位置: 下面是我的矩形类: 我引用的内容: 问题: 为什么在矩形视图上没有检测到单击?您已经告诉视图当用户单击它时该做什么,但看起来该视图当前不可单击。试试这个: topRectangle

我有两个矩形,每个矩形正好占据屏幕的一半,我试图检测矩形上的点击

我的尝试: 我试图在两个矩形上都设置OnClickListener,但当我单击任一矩形时,都不会检测到单击。我非常确定它没有检测到点击,所以我登录到了监听器中,这证实了我的怀疑;未输入侦听器,因此未检测到任何单击

下面是我在活动中声明侦听器的位置:

下面是我的矩形类:

我引用的内容:

问题:
为什么在矩形视图上没有检测到单击?

您已经告诉视图当用户单击它时该做什么,但看起来该视图当前不可单击。试试这个:

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

在我提供的活动代码的最后一行中,我为两个矩形编写了
setClickable
。无论如何,我还是用了一个触摸式监听器解决了这个问题。谢谢!:)
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;
}

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