2010-09-07 146 views
0

我想在屏幕上识别触发触摸事件

FrameLayout main = (FrameLayout) findViewById(R.id.main_view); 
final Ball bigball; 
bigball = new Ball(this,50,50,25, 0xFFFF0000); 
main.addView(bigball); 

这画我的屏幕上的红色圆圈拖动视图的视图。如果我需要如何以及在哪里实现我的触摸屏处理,如果我想让只有才会触发红色圆圈而不是屏幕的其余部分?

Ball.Java是一个单独的类:

public class Ball extends View 

我知道我可以检查所在的坐标使用者的触摸屏幕,然后比较,为那里的圆,但必须有一个办法简单地检查ID或圆的其他参考,对吧?

到目前为止,我已经看过并尝试过的所有内容都会运行我的代码,而不管触摸在屏幕上的何处开始或停止。 (所以我可以移动球,我可以用手指和任何东西在屏幕上画线,但我不知道是否触摸了红色圆圈。)

任何人都可以用简单的语言将其展开给我吗? (原谅我在我的问题中使用同样简单的单词,我不再觉得自己在这里做了什么,而现在我也不会再正确地使用像“听众”或“事件”这样的大词。

感谢。

回答

0

可以覆盖/在从搜索扩展Ball类实现的onTouchEvent。

public class Ball extends View { 
    public boolean onTouchEvent(MotionEvent ev) { 
    if(ev.getAction == /*check against all the desired action*/ { 
     //handle touch and return true 
    return super.onTouchEvent(ev); 
}

+0

是的,我tohght这应该工作了。 – Alex 2010-09-07 08:59:59

+0

但无论在哪里,我点击调用该函数。是否有可能我的球只是占据了整个屏幕,并且我一直在追逐错误类型的错误? 这是一个在画布上绘制的ciclre: – Alex 2010-09-07 09:02:42

+0

public class Ball extends View { private float x; 私人浮动y; private final int r; private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); (上下文上下文,float x,float y,int r,int bcolor){super(context);}};}}}}}}}。 mPaint.setColor(bcolor); this.x = x; this.y = y; this.r = r; } @Override protected void onDraw(Canvas canvas){ super.onDraw(canvas); canvas.drawCircle(x,y,r,mPaint); – Alex 2010-09-07 09:03:38