6

我有一个图像视图,我需要长按(用于上下文菜单)和常规按工作。我可以让一个或另一个工作,但不能同时工作。我错过了什么?下面的代码仅适用于普通印刷机。我一触摸屏幕,它就开始执行 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));
    }
 }
4

3 回答 3

20

您可以尝试这样做:

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;
    }
});
于 2012-05-08T17:49:14.753 回答
3

经过大量观察和试验,这是我发现实现这种行为的最佳解决方案:

//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;
      }
    });
于 2014-05-19T21:13:00.720 回答
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() ;
        }
      }
于 2012-05-08T18:30:00.293 回答