我是android编程的新手。我的代码需要帮助。正如你所看到的,我有一个菜单,在点击一个按钮后,我使用onDraw方法和onTouchEvent制作。
public boolean onTouchEvent(MotionEvent event)
{
CoordCheck cc = new CoordCheck();
Log.d(TAG, "Coords: X=" + event.getX() + ",Y=" + event.getY()+"choice="+glb.getChoice());
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
float x, y;
x = event.getX();
y = event.getY();
//
// MAIN MENU
//
if(glb.getChoice()==0)
{
if (cc.ifMenu_exit(x, y))
{
thread.setRunning(false);
((Activity)getContext()).finish();
}
else if (cc.ifMenu_pagsasanay(x,y))
{
thread.menu_pagsasanay();
}
}
//
// PAGSASANAY MENU
//
if(glb.getChoice()==1)
{
Log.d(TAG,"PAGSASANAY MENU");
if (cc.ifPagsasanay_pitik(x, y))
{
pitik = new PitikBulagAnimation(
BitmapFactory.decodeResource(getResources(), R.drawable.hand_sprites)
, 450, 60 // initial position
, 350, 41 // width and height of sprite
, 50, 6); // FPS and number of frames in the animation
Log.d(TAG,"pitik");
thread.pitikAnimation();
}
else if (cc.ifPagsasanay_jnp(x, y))
{
jnp = new JNPAnimation(
BitmapFactory.decodeResource(getResources(), R.drawable.ai_sprite)
, 550, 70 // initial position
, 283, 41 // width and height of sprite
, 50, 4); // FPS and number of frames in the animation
Log.d(TAG,"jnp");
thread.jnpAnimation();
}
if(cc.ifBackButton(x, y))
{
thread.menu_main();
}
呃。。glb.getChoice如果值为零,则表示视图包含主菜单。我的问题是,如果我点击了"pagsasanay"按钮(在主菜单内),我可以转到Pagsasanai菜单,glb类中的选项将设置为1,但到达那里后,event.getX()值仍然存在,因此点击了另一个按钮,该按钮与"pagsanay"按钮
这些用作按钮的视图应该在xml中设置onClick属性。onClick属性标识单击视图时要调用的方法,在该方法中,您可以决定要执行的操作、提供新菜单或其他操作。如果你更倾向于java,那么你可以为每个视图编写onClickListeners,但imho只是需要编写更多的代码。另一方面,有时对侦听器进行编码是有意义的,这取决于你在做什么。从我目前所看到的代码来看,选择xml定义的onClick属性。