我有两个矩形,每个矩形完全占据屏幕的一半,我试图检测矩形上的点击。
我尝试在两个矩形上设置OnClickListener,但是当我单击任一矩形时,将无法检测到单击。我非常确定它没有检测到点击,所以我登录了听众,这证实了我的怀疑;没有输入监听器,因此没有检测到任何点击。
以下是我在活动中声明听众的地方:
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);
这是我对矩形的类:
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;
}
}
Android: onClickListener does not work as programmed
为什么矩形视图上没有检测到点击?
答案 0 :(得分:0)
您已告知视图,当用户点击它时该怎么做,但看起来该视图当前不可点击。试试这个:
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);