onTouch, onLongClick together in android(onTouch,onLongClick一起在android中)
问题描述
我正在动态地将图像视图添加到父布局.我正在对添加的图像执行放大/缩小操作.我想删除它的添加视图 onLongPress.
I'am adding imageviews to parent layout dynamically. And i'am performing zoom in/out operations onTouch of added image. I want to remove the added view onLongPress of it.
img.setOnLongClickListener(longClickAction);
img.setOnTouchListener(touchAction);
长按:
OnLongClickListener longClickAction = new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
parentLayout.removeView((ImageView)v);
return false;
}
};
触摸:
OnTouchListener touchAction = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView i = (ImageView)v;
//perfrom zoom operation on touch of imageview
zoom(i, event);
return true;
}
};
只有 Touch 事件有效.为什么?我怎么能两者兼得?我哪里出错了?我应该怎么做才能删除添加的视图?请帮我.提前致谢.
Only Touch events are working. Why? How can i have both? Where iam going wrong? What should i do to remove added view? Please help me. Thanks in advance.
推荐答案
onTouch
总是为您的视图调用,因为这是将事件分派到视图的初始状态.当你长按你的视图时,它仍然首先调用 onTouch
并且因为你在 onTouch
中返回 true
(这意味着你已经消费了这个事件并且它不应该被进一步发送)你不会得到 onLongPress
调用.诀窍是在 onTouch
onTouch
is always called for your view since this is the initial state of dispatching the events to the view. When you long press your view this still calls onTouch
first and since you return true
in onTouch
(which means that you've consumed this event and it should not be further dispatched) you won't get onLongPress
called. What will do the trick is returning false
in onTouch
这篇关于onTouch,onLongClick一起在android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:onTouch,onLongClick一起在android中
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01