OnTouch没有开火

时间:2011-04-18 07:19:24

标签: android android-layout

我是Android新手,我试图隐藏图片,但似乎OnTouch没有被解雇,有人可以指出我正确的方向吗?

谢谢

public class GameActivity extends Activity {    
/** Called when the activity is first created. */ 
//GameView theView;
@Override  
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);
    //theView = new GameView(this);
    //theView.setBackgroundResource(R.layout.myviewlay);
    setContentView(R.layout.myviewlay);

  }
public boolean onTouch(View view,MotionEvent event)
{

         ImageView image = (ImageView) findViewById(R.id.ImageView03);
         image.setVisibility(View.INVISIBLE);
         view.invalidate();  
         return true;
}

}

2 个答案:

答案 0 :(得分:4)

覆盖OnTouchEvent可以解决您的问题。

@Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        int action = event.getAction();
        if(action == MotionEvent.ACTION_DOWN){
            ImageView image = (ImageView) findViewById(R.id.ImageView03);
            image.setVisibility(View.INVISIBLE);
        }
        return super.onTouchEvent(event);
    }

答案 1 :(得分:0)

在ImageView上设置事件侦听器可以查找触摸的图像。

ImageView imageView = (ImageView) findViewById(R.id.Image01);
            imageView.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    // do your event here.
                }
            });

            imageView.setOnTouchListener(new OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    return false;
                }
            });