开发者

OnTouch not Firing

开发者 https://www.devze.com 2023-02-25 07:38 出处:网络
I am new to A开发者_运维知识库ndroid and I am trying to hide an image, but it seems the OnTouch is not being fired, can anyone please point me in the right direction?

I am new to A开发者_运维知识库ndroid and I am trying to hide an image, but it seems the OnTouch is not being fired, can anyone please point me in the right direction?

Thank you

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;
}

}


Override OnTouchEvent fixes your issue.

@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);
    }


Setting event listener on ImageView enables you to find the image you touched.

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;
                }
            });
0

精彩评论

暂无评论...
验证码 换一张
取 消