[英]android I dont understand Long press detection in my subclass ImageView
hi你好
Im new to the Touchscreen programming please give me some help!我是触摸屏编程的新手,请给我一些帮助!
I have the:我有:
public class PhotoEditDrawView extends ImageView {
and i have the:我有:
@Override
public boolean onTouchEvent(MotionEvent event) {
In the constructor i have the:在构造函数中,我有:
setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(ctx, "hello hello ", Toast.LENGTH_SHORT).show();
return true;
}
});
The onLongClick
is never fired. onLongClick
永远不会被解雇。 What am i doing wrong?我究竟做错了什么?
Everything in the onTouchEvent
is working good. onTouchEvent
中的所有内容都运行良好。
What i want to do is start an Activity with @android:style/Theme.Dialog
when pressing 1-2 second.我想要做的是在按下 1-2 秒时使用
@android:style/Theme.Dialog
启动一个 Activity。
take a look at this little snippet, it works!看看这个小片段,它有效!
public class MyImageView extends ImageView {
private Context mContext;
public MyImageView(Context context) {
super(context);
setBackgroundColor(Color.RED);
mContext = context;
setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(mContext, "hello hello ", Toast.LENGTH_SHORT).show();
return true;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return super.onTouchEvent(event);
}
}
make sure you´re returning true in onTouchEvent and onLongClick, so that the events keep firing.确保在 onTouchEvent 和 onLongClick 中返回 true,以便事件继续触发。
I have had exactly the same problem with an ImageView subclass, the onTouch event fired OK but I couldn't get a Long Press to register with the OnLongClickListener.我在 ImageView 子类中遇到了完全相同的问题,onTouch 事件触发正常,但我无法通过长按来注册 OnLongClickListener。 In the end I just called System.currentTimeMillis() in the MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP events to calculated the time difference myself.
最后我只是在 MotionEvent.ACTION_DOWN 和 MotionEvent.ACTION_UP 事件中调用 System.currentTimeMillis() 来自己计算时间差。 Not perfect, but it worked around the problem & it works.
不完美,但它解决了这个问题并且它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.