我有一个imageview,我需要长按(用于上下文菜单)和常规印刷机工作。我可以让一个或另一个工作,但不是两个。我错过了什么?以下代码仅适用于常规印刷机。一触摸屏幕,它就会开始执行onTouch代码。
ImageView image = (ImageView)findViewById(R.id.visible_image);
image.setLongClickable(true);
image.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// disable the screen taps for 500ms
DecodeActionDownEvent(v, ev, bm2);
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
return true;
}
});
registerForContextMenu(image);
...
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Edit");
ArrayList<String> menuItems = new ArrayList<String>();
menuItems.add("Edit page settings");
menuItems.add("Edit page objects");
menuItems.add("Edit this object");
for (int i = 0; i<menuItems.size(); i++) {
menu.add(Menu.NONE, i, i, menuItems.get(i));
}
}
答案 0 :(得分:19)
您可以尝试这样做:
ImageView imageView = (ImageView) findViewById(R.id.ImageView);
imageView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//your stuff
return true;
}
});
imageView.setOnClickListener(new OnClickListener() {
@Override
public boolean onClick(View v) {
//your stuff
return true;
}
});
答案 1 :(得分:2)
要获取x和y坐标,请设置触摸侦听器
ImageView.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
//And u can get x and y values like:
x = event.getX();
y = event.getY() ;
}
}
答案 2 :(得分:2)
经过多次研究和实验,这是我发现实现此类行为的最佳解决方案:
//Define these variables at the beginning of your Activity or Fragment:
private long then;
private int longClickDuration = 5000; //for long click to trigger after 5 seconds
...
ImageView imageView = (ImageView) findViewById(R.id.visible_image);
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
then = (long) System.currentTimeMillis();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if ((System.currentTimeMillis() - then) > longClickDuration) {
/* Implement long click behavior here */
System.out.println("Long Click has happened!");
return false;
} else {
/* Implement short click behavior here or do nothing */
System.out.println("Short Click has happened...");
return true;
}
}
return true;
}
});