如何配置LongClick的响应时间?[英] How to config the response time for LongClick?

本文是小编为大家收集整理的关于如何配置LongClick的响应时间?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

在Android, View.onLongClickListener() 大约1秒把它视为一个长点击.如何为长点击配置响应时间?

推荐答案

默认超时 ViewConfiguration.getLongPressTimeout()

您可以实现自己的长按:

boolean mHasPerformedLongPress;
Runnable mPendingCheckForLongPress;

@Override
public boolean onTouch(final View v, MotionEvent event) {

    switch (event.getAction()) {
        case MotionEvent.ACTION_UP:

            if (!mHasPerformedLongPress) {
                    // This is a tap, so remove the longpress check
                    if (mPendingCheckForLongPress != null) {
                        v.removeCallbacks(mPendingCheckForLongPress);
                    }
                // v.performClick();
            }

            break;
        case  MotionEvent.ACTION_DOWN:
            if( mPendingCheckForLongPress == null) {  
                mPendingCheckForLongPress = new Runnable() {
                    public void run() {
                        //do your job
                    }
                };
            }


            mHasPerformedLongPress = false;
            v.postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout());

            break;
        case MotionEvent.ACTION_MOVE:
            final int x = (int) event.getX();
            final int y = (int) event.getY();

            // Be lenient about moving outside of buttons
            int slop = ViewConfiguration.get(v.getContext()).getScaledTouchSlop();
            if ((x < 0 - slop) || (x >= v.getWidth() + slop) ||
                (y < 0 - slop) || (y >= v.getHeight() + slop)) {

                if (mPendingCheckForLongPress != null) {
                    v. removeCallbacks(mPendingCheckForLongPress);
                }
            }
            break;
        default:
            return false;
    }

    return false;
} 

本文地址:https://www.itbaoku.cn/post/102349.html

问题描述

In Android, View.onLongClickListener() takes about 1 sec to treat it as a long click. How can I configure the response time for a long click?

推荐答案

The default time out is defined by ViewConfiguration.getLongPressTimeout().

You can implement your own long press:

boolean mHasPerformedLongPress;
Runnable mPendingCheckForLongPress;

@Override
public boolean onTouch(final View v, MotionEvent event) {

    switch (event.getAction()) {
        case MotionEvent.ACTION_UP:

            if (!mHasPerformedLongPress) {
                    // This is a tap, so remove the longpress check
                    if (mPendingCheckForLongPress != null) {
                        v.removeCallbacks(mPendingCheckForLongPress);
                    }
                // v.performClick();
            }

            break;
        case  MotionEvent.ACTION_DOWN:
            if( mPendingCheckForLongPress == null) {  
                mPendingCheckForLongPress = new Runnable() {
                    public void run() {
                        //do your job
                    }
                };
            }


            mHasPerformedLongPress = false;
            v.postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout());

            break;
        case MotionEvent.ACTION_MOVE:
            final int x = (int) event.getX();
            final int y = (int) event.getY();

            // Be lenient about moving outside of buttons
            int slop = ViewConfiguration.get(v.getContext()).getScaledTouchSlop();
            if ((x < 0 - slop) || (x >= v.getWidth() + slop) ||
                (y < 0 - slop) || (y >= v.getHeight() + slop)) {

                if (mPendingCheckForLongPress != null) {
                    v. removeCallbacks(mPendingCheckForLongPress);
                }
            }
            break;
        default:
            return false;
    }

    return false;
}