How long is the event onLongPress in the Android?

余生长醉 提交于 2019-12-17 15:53:22

问题


Android supports an event onLongPress. The question I have is 'how long' (in milliseconds) is the 'press' to trigger the event?


回答1:


The standard long press time is what is returned by getLongPressTimeout(), which is currently 500ms but may change (in 1.0 it was 1000ms but changed in a later release; maybe in the future it will be user-customizable).

The browser uses its own long press time because it has some more complicated interactions. I believe this should be 1000, though again it may change in the future. It is not adding the different timeouts together.




回答2:


You can use the getLongPressTimeout method in android.view.ViewConfiguration to programmatically determine this value.

See the docs for details.




回答3:


Generally, like Roman Nurik mentioned, you can use ViewConfiguration.getLongPressTimeout() to programmatically obtain long press value value. The default value is 500ms.

/**
 * Defines the default duration in milliseconds before a press turns into
 * a long press
 */
private static final int DEFAULT_LONG_PRESS_TIMEOUT = 500;

But, the long press duration is customizable globally by setting it in accessibility. Values are Short (400 ms), Medium (1000 ms) or Long (1500 ms). You can see its source code in Settings:

// Long press timeout.
mSelectLongPressTimeoutPreference =
        (ListPreference) findPreference(SELECT_LONG_PRESS_TIMEOUT_PREFERENCE);
mSelectLongPressTimeoutPreference.setOnPreferenceChangeListener(this);
if (mLongPressTimeoutValueToTitleMap.size() == 0) {
    String[] timeoutValues = getResources().getStringArray(
            R.array.long_press_timeout_selector_values);
    mLongPressTimeoutDefault = Integer.parseInt(timeoutValues[0]);
    String[] timeoutTitles = getResources().getStringArray(
            R.array.long_press_timeout_selector_titles);
    final int timeoutValueCount = timeoutValues.length;
    for (int i = 0; i < timeoutValueCount; i++) {
        mLongPressTimeoutValueToTitleMap.put(timeoutValues[i], timeoutTitles[i]);
    }
}



回答4:


This is what R.array.long_press_timeout_selector_titles look like:

    <!-- Titles for the list of long press timeout options. -->
    <string-array name="long_press_timeout_selector_titles">
        <!-- A title for the option for short long-press timeout [CHAR LIMIT=25] -->
        <item>Short</item>
        <!-- A title for the option for medium long-press timeout [CHAR LIMIT=25] -->
        <item>Medium</item>
        <!-- A title for the option for long long-press timeout [CHAR LIMIT=25] -->
        <item>Long</item>
    </string-array>
    <!-- Values for the list of long press timeout options. -->
    <string-array name="long_press_timeout_selector_values" translatable="false">
        <item>400</item>
        <item>1000</item>
        <item>1500</item>
    </string-array>



回答5:


Hmmm ... I was hoping to get the accumulative time. As far as I can tell, getLongPressTimeout(), is the component time that is added to when event-press is determined to be start, plus TAP_TIMEOUT, plus ??? and then 1000ms if in the web browser.

I have calculated it to be 1650ms but I would like to have some confirmation of the resultant value. The reason is that I need something that is not integrated with the SDK to predict the long-hold.

I believe the value from getLongPressTimeout is 500ms, but the gesture clearly takes longer -- closer to 2 seconds.




回答6:


View (and therefore most of its subclasses) uses getLongPressTimeout. Perhaps the default timeout was not sufficient in the browser.



来源:https://stackoverflow.com/questions/1930895/how-long-is-the-event-onlongpress-in-the-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!