2012-02-15 28 views
0

我尝试为android开发一个应用程序。我有以下代码:在仿真器中测试触摸功能

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    CardImage = (ImageView)findViewById(R.id.CardImage); 
    CardImage.setOnTouchListener(this); 
} 

public boolean onTouch(View v, MotionEvent event) 
{ 
    if(event.getAction() == MotionEvent.ACTION_UP) 
    { 
     System.out.println("Touch!"); 
    } 

    return true; 
} 

如何在模拟器中调试Ontouch函数?我试图拖动图像切换鼠标,但没有发生。

感谢您的帮助!

+0

你活动实现onTouchListener吗? – 2012-02-15 16:26:15

回答

0

试试这个代码(注:未测试)

CardImage.setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      int X = (int) event.getX(); 
      int Y = (int) event.getY(); 
      int action = event.getAction(); 
      if (action == MotionEvent.ACTION_UP){ 
      TranslateAnimation anim = new TranslateAnimation(CardImage.getLeft(),X,CardImage.getTop() 
      ,Y); 
      anim.setFillAfter(true); 
      anim.setDuration(1000); 
      CardImage.startAnimation(anim); 
      } 
      return true; 
     } 
     }); 
0

它只是用下面的代码工作。

public class TestTouch extends Activity implements View.OnTouchListener{  
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ImageView iv = new ImageView(this); 
    iv.setImageResource(R.drawable.flag); // my image 
    iv.setOnTouchListener(this); 
    setContentView(iv); 
} 

public boolean onTouch(View v, MotionEvent event) 
{ 
    System.out.println(event.getAction()); 
    if(event.getAction() == MotionEvent.ACTION_UP) 
    { 
     System.out.println("Touch!"); 
    }  
    return true; 
}  
}