android 上如何判断长按?

发布于 2024-11-17 22:54:48 字数 2038 浏览 2 评论 0原文

我正在寻找一种方法,当用户长时间触摸地图视图(比如说 1000 毫秒)时,我可以知道如何执行某个操作。

我将如何判断用户触摸地图视图(或任何视图)的时间长短。

它类似于 Android 谷歌地图应用程序,当您长按时,它会弹出一个气球覆盖项目。

编辑添加

mapView.setOnLongClickListener(new View.OnLongClickListener() {

        public boolean onLongClick(View v) {

            Toast.makeText(mapView.getContext(), "Hello 123", 2000);
            return false;
        }
    });

上述内容不起作用...有什么想法为什么吗?

编辑添加

这是我目前正在尝试的,但它似乎不起作用,即使我只按手机,它说该事件是一个action_move,

我在我的MapActivity中使用内部类

    private long startTime=0;
private long endTime=0;

class MapOverlay extends Overlay {



    @Override
    public boolean onTouchEvent(MotionEvent ev, MapView mapView) {

        if(ev.getAction() == MotionEvent.ACTION_DOWN){
             //record the start time
             startTime = ev.getEventTime();

             Log.d("LC", "IN DOWN");
          }else if(ev.getAction() == MotionEvent.ACTION_UP){
             //record the end time
             endTime = ev.getEventTime();
             Log.d("LC", "IN UP");
          }else if(ev.getAction() == MotionEvent.ACTION_MOVE){
              Log.d("LC", "IN move");
              endTime=0;
          }

          //verify
          if(endTime - startTime > 1000){
             //we have a 1000ms duration touch
             //propagate your own event
              Log.d("LC", "time touched greater than 1000ms");
              Toast.makeText(getBaseContext(), "Hello 123", Toast.LENGTH_SHORT).show();
              startTime=0; 
              endTime=0;
             return true; //notify that you handled this event (do not propagate)
          }

        return false;//propogate to enable drag

    }

}

,这是我的错误日志对我来说没有任何意义,

06-29 14:29:55.509: DEBUG/LC(7693): IN move
06-29 14:29:56.149: DEBUG/LC(7693): IN UP
06-29 14:29:56.149: DEBUG/LC(7693): 6346707 6349261
06-29 14:29:56.149: DEBUG/LC(7693): time touched greater than 1000ms

结束时间应该设置为零......但它不是......知道为什么吗?

I am looking for a way for when a user long touches a mapview (lets say for 1000ms) that i can some how do a certain action.

How would i go about judging how long a user long touches a mapsview(or any view).

It would be similar to android google maps app, when you long touch, it brings up a balloon overlay item.

Edit added

mapView.setOnLongClickListener(new View.OnLongClickListener() {

        public boolean onLongClick(View v) {

            Toast.makeText(mapView.getContext(), "Hello 123", 2000);
            return false;
        }
    });

the above does not work... any ideas why?

Edit added

This is what i am trying at the moment, but it does not seem to work, even if i only press on the phone, it says the event is an action_move,

i am using an inner class in my MapActivity

    private long startTime=0;
private long endTime=0;

class MapOverlay extends Overlay {



    @Override
    public boolean onTouchEvent(MotionEvent ev, MapView mapView) {

        if(ev.getAction() == MotionEvent.ACTION_DOWN){
             //record the start time
             startTime = ev.getEventTime();

             Log.d("LC", "IN DOWN");
          }else if(ev.getAction() == MotionEvent.ACTION_UP){
             //record the end time
             endTime = ev.getEventTime();
             Log.d("LC", "IN UP");
          }else if(ev.getAction() == MotionEvent.ACTION_MOVE){
              Log.d("LC", "IN move");
              endTime=0;
          }

          //verify
          if(endTime - startTime > 1000){
             //we have a 1000ms duration touch
             //propagate your own event
              Log.d("LC", "time touched greater than 1000ms");
              Toast.makeText(getBaseContext(), "Hello 123", Toast.LENGTH_SHORT).show();
              startTime=0; 
              endTime=0;
             return true; //notify that you handled this event (do not propagate)
          }

        return false;//propogate to enable drag

    }

}

and here is my error log that does not make any sense to me

06-29 14:29:55.509: DEBUG/LC(7693): IN move
06-29 14:29:56.149: DEBUG/LC(7693): IN UP
06-29 14:29:56.149: DEBUG/LC(7693): 6346707 6349261
06-29 14:29:56.149: DEBUG/LC(7693): time touched greater than 1000ms

the end time should be set to zero...but it is not...any idea why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

明天过后 2024-11-24 22:54:48

这就是通常创建 onLongClickListener 的方式。试试这个:

mapView.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View arg0) {

            Toast.makeText(mapView.getContext(), "Hello 123", 2000);

            return false;

        }
    });

参考您的编辑:

这可能是获得您想要的东西的方法。

private final Handler handler = new Handler();
private final Runnable runnable = new Runnable() {
public void run() {
     checkGlobalVariable();
}
};

// Other init stuff etc...

@Override
public void onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
    // Execute your Runnable after 1000 milliseconds = 1 second.
    handler.postDelayed(runnable, 1000);
    mBooleanIsPressed = true;
}

if(event.getAction() == MotionEvent.ACTION_UP) {
    if(mBooleanIsPressed) {
        mBooleanIsPressed = false;
        handler.removeCallbacks(runnable);
    }
}
}

现在您可以使用 checkGlobalVariable 函数进行检查:

if(mBooleanIsPressed == true)

这就是处理这种情况的方法。祝你好运。

This is how do you normally create an onLongClickListener. Try this:

mapView.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View arg0) {

            Toast.makeText(mapView.getContext(), "Hello 123", 2000);

            return false;

        }
    });

Reference to your edit:

This might be the way to get what you want.

private final Handler handler = new Handler();
private final Runnable runnable = new Runnable() {
public void run() {
     checkGlobalVariable();
}
};

// Other init stuff etc...

@Override
public void onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
    // Execute your Runnable after 1000 milliseconds = 1 second.
    handler.postDelayed(runnable, 1000);
    mBooleanIsPressed = true;
}

if(event.getAction() == MotionEvent.ACTION_UP) {
    if(mBooleanIsPressed) {
        mBooleanIsPressed = false;
        handler.removeCallbacks(runnable);
    }
}
}

And now you can check with checkGlobalVariable function:

if(mBooleanIsPressed == true)

This is how you can handle this case. Good luck.

暮色兮凉城 2024-11-24 22:54:48

您可能正在寻找正常的长按?
您必须通过将 android:longClickable 添加到视图 xml 或调用 setLongClickable(true) 将视图设置为可长时间点击。
然后您可以将 OnLongClickListener 添加到视图中。
我不知道有什么方法可以准确确定长点击的时间。但默认的长按与您提到的谷歌地图长按相同。

OnLongClickListener

Your are probably looking for a normal long click?
You will have to set your view to be long clickable by adding android:longClickable to your views xml, or by calling setLongClickable(true).
Then you can add an OnLongClickListener to the view.
I dont know of a way to determine exactly how long the long click is. But the default long click is the same as the google maps long click that you mentioned.

OnLongClickListener

夕嗳→ 2024-11-24 22:54:48

您可以设置一个 longClickListener 和一个 touchListener。添加一个布尔类数据成员变量 longClicked 并将其初始设置为 false。这是设置 longClickListener 的方法。

    view.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            longClicked = true;
            return false;
        }
    });

对于 touchListener

    view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(longClicked){
                //Do whatever you want here!!
                longClicked = false;
            }
            return false;
        }
    });

它将给你带来与谷歌地图长按相同的效果。希望有帮助。

You can set up a longClickListener and a touchListener. Add a boolean class data member variable longClicked and set it to false initially. This is how you can set the longClickListener.

    view.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            longClicked = true;
            return false;
        }
    });

For touchListener

    view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(longClicked){
                //Do whatever you want here!!
                longClicked = false;
            }
            return false;
        }
    });

It will give you the same effect as google maps long click. Hope it helps.

日暮斜阳 2024-11-24 22:54:48

使用 System.currentTimeMillis() 代替 ev.getEventTime();

use System.currentTimeMillis() instead of ev.getEventTime();

梦里寻她 2024-11-24 22:54:48

MotionEvent.ACTION_UP 时,endTime 将被设置为 ev.getEventTime(),这使得当 MotionEvent.ACTION_MOVE 时将 endTime 设置为零 不受影响。
您不应将 endTime 设置为零,而应将 startTime 设置为 ev.getEventTime()

When MotionEvent.ACTION_UP, endTime will be set to ev.getEventTime(), this make setting endTime to zero when MotionEvent.ACTION_MOVE be not affect.
Instead of setting endTime to zero, you should set startTime to ev.getEventTime()

吻安 2024-11-24 22:54:48

这就是我使用长触摸和短触摸的方式

View.setOnTouchListener(new View.OnTouchListener() {
        double firsttouch=0;
        boolean isup=false;
        int millistotouch=1000;
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction()==MotionEvent.ACTION_DOWN) {
                firsttouch = (double) System.currentTimeMillis();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (!isup) {
                             //
                             // Do your long click work
                             //
                            }
                            else
                            {
                                firsttouch=0;
                                isup=false;
                            }
                        }
                    }
                }, millistotouch);
                return true;
            }
            if (motionEvent.getAction()==MotionEvent.ACTION_UP) {
                if (((double) System.currentTimeMillis()-firsttouch)<millistotouch)
                {
                    isup=true;
                    //
                    // Do your short click work
                    //
                }

            }
            return false;
        }
    });

This is how I use long and short touches

View.setOnTouchListener(new View.OnTouchListener() {
        double firsttouch=0;
        boolean isup=false;
        int millistotouch=1000;
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction()==MotionEvent.ACTION_DOWN) {
                firsttouch = (double) System.currentTimeMillis();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (!isup) {
                             //
                             // Do your long click work
                             //
                            }
                            else
                            {
                                firsttouch=0;
                                isup=false;
                            }
                        }
                    }
                }, millistotouch);
                return true;
            }
            if (motionEvent.getAction()==MotionEvent.ACTION_UP) {
                if (((double) System.currentTimeMillis()-firsttouch)<millistotouch)
                {
                    isup=true;
                    //
                    // Do your short click work
                    //
                }

            }
            return false;
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文