2012-02-15 69 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; 
}  
}