简体   繁体   English

OnTouchListener不起作用

[英]OnTouchListener not working

I have a few buttons in a tab layout for which the OnClickListener works fine. 我在选项卡布局中有一些按钮,OnClickListener可以正常工作。 The purpose of these buttons is to start a new activity when clicked upon. 这些按钮的目的是在单击时启动新的活动。 I want my code to work for the touch mode of the phone as well. 我也希望我的代码也适用于手机的触摸模式。 In other words I wanted to know how to implement the OnTouchListener for the code. 换句话说,我想知道如何为代码实现OnTouchListener。 Currently I have something like 目前我有类似

public boolean onTouch(View v, MotionEvent event) {
  startActivity(new Intent().setClass(this, OtherActivity.class));
  return true;
}

I am just thinking about it the same way as I would think about the onClick() method but apparently I am wrong. 我只是以与onClick()方法相同的方式来考虑,但是显然我错了。

Any help in this matter would be appreciated. 在这方面的任何帮助将不胜感激。 Thanks! 谢谢!

Touching a button on the screen should generate click events the same as using a hardware keypad. 触摸屏幕上的按钮将产生与使用硬件键盘相同的单击事件。 Try using the Log object and LogCat to see when methods are called. 尝试使用Log对象和LogCat查看何时调用方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM