我正在實現一個簡單的自定義視圖,基本上是爲了獲得一個android canvas的掛件。這是一個簡單的tictactoe板。onTouchEvent沒有得到執行
這是自定義視圖類:
public class BoardInterface extends View implements View.OnTouchListener{
private int board_width, board_height;
private final Context context;
public BoardInterface(Context context) {
super(context);
this.context = context;
}
@Override
protected void onDraw(Canvas canvas) {
board_width = canvas.getWidth();
board_height = canvas.getHeight();
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(20);
canvas.drawLine((board_width/5)*2, (board_height/6), (board_width/5)*2, ((board_height/6)*5), paint);
canvas.drawLine(((board_width/5)*3), (board_height/6), ((board_width/5)*3), ((board_height/6)*5), paint);
canvas.drawLine((board_width/6), ((board_height/7)*3), ((board_width/6)*5), ((board_height/7)*3), paint);
canvas.drawLine((board_width/6), ((board_height/7)*4), ((board_width/6)*5), ((board_height/7)*4), paint);
super.onDraw(canvas);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int x, y;
Log.d("TOUCH", "WORKS");
if (event.getAction() == MotionEvent.ACTION_DOWN) {
x = (int) Math.floor(event.getX());
y = (int) Math.floor(event.getY());
Toast.makeText(context, String.valueOf(x) + "," + String.valueOf(y), Toast.LENGTH_SHORT).show();
}
return super.onTouchEvent(event);
}
}
我給自己定一個OnTouchListener並覆蓋到展示一個簡單的烤麪包。 屏幕上沒有任何東西出現,即toas,也不會在logcat中獲得Log.D()消息。
我在這裏錯過了什麼?