How to get imageView inside onTouchEvent(MotionEvent event) in android

孤人 提交于 2019-12-12 17:16:54

问题


I defined onTouchEvent(MotionEvent event) to receive touch events for imageview. I am using multiple imageviews and i have to find for which imageview I have received touch event. Is it possible to get imageview inside onTouchEvent?

public void createImageViews()
{
  int i = 0;

  imageArray[0] = (ImageView)findViewById(R.id.image1);
  imageArray[1] = (ImageView)findViewById(R.id.image2);
  imageArray[2] = (ImageView)findViewById(R.id.image3);
  imageArray[3] = (ImageView)findViewById(R.id.image4);
  imageArray[4] = (ImageView)findViewById(R.id.image5);
  imageArray[5] = (ImageView)findViewById(R.id.image6);

  for (i = 0; i < count; i++)
  {
    imageArray[i].setOnTouchListener(this);
  } 
}
 public class Touch extends Activity implements OnTouchListener, AnimationListener { }

I have tried using

 public boolean onTouch(View v, MotionEvent event) { } but not receiving the touch events.

After starting the animation on imageview I did not observe touch events in onTouch() and getting touch events in onTouchEvent() . imgArray[g_animCount].startAnimation(movArray[g_animCount]);


回答1:


use the below sample code snippet::::

    public class MyImageView extends Activity implements OnTouchListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lay_name);        
        int i = 0;
        imageArray[0] = (ImageView)findViewById(R.id.image1);
        imageArray[1] = (ImageView)findViewById(R.id.image2);
        imageArray[2] = (ImageView)findViewById(R.id.image3);
        imageArray[3] = (ImageView)findViewById(R.id.image4);
        imageArray[4] = (ImageView)findViewById(R.id.image5);
        imageArray[5] = (ImageView)findViewById(R.id.image6);

        for (i = 0; i < count; i++)  {
            imageArray[i].setOnTouchListener(this);
         } 


    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.image1:
        //your stuff
        break;
        case R.id.image2:
        //your stuff        
        break;
        case R.id.image3:
        //your stuff
        break;
        case R.id.image4:
         //your stuff
        break;
        case R.id.image5:
         //your stuff
        break;
        case R.id.image6:
         //your stuff
        break;
        }
    }

}




回答2:


You can overide onTouchListener. The code for onTouchListener is

@Override
public boolean onTouch(View v, MotionEvent event) {
/// your stuff
}

use View v to know the touched view




回答3:


You can set android:onClick="clickHandler" for each View in your layout xml and define a method public void clickHandler(View view) in your Activity to handle the touch/click events.

This was introduced in 1.6. Check this LINK for more detail.



来源:https://stackoverflow.com/questions/10251002/how-to-get-imageview-inside-ontoucheventmotionevent-event-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!