在模拟器中测试触摸功能

发布于 2025-01-06 15:12:27 字数 526 浏览 2 评论 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功能?我尝试用鼠标拖动图像切换,但没有任何反应。

谢谢你的帮助!

I try to develop a App for android. I have the following code:

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

How can i debug the Ontouch function in the emulator? I tried to drag the image switch the mouse but nothing happend.

Thanks for you help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花之痕靓丽 2025-01-13 15:12:27

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

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

Try this code (Note : not tested)

 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;
         }
        });
給妳壹絲溫柔 2025-01-13 15:12:27

它刚刚与以下代码一起使用。

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

It has just worked with the following code.

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;
}    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文