Android中的onLongPress事件有多长?

发布于 2024-08-15 14:05:18 字数 63 浏览 4 评论 0原文

Android 支持 onLongPress 事件。我的问题是“按下”触发事件需要“多长时间”(以毫秒为单位)?

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

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

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

发布评论

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

评论(6

德意的啸 2024-08-22 14:05:18

标准长按时间是 getLongPressTimeout() 返回的时间,目前为 500ms,但可能会发生变化(在 1.0 中为 1000ms,但在以后的版本中发生了变化;也许将来可以由用户自定义)。

浏览器使用自己的长按时间,因为它有一些更复杂的交互。我相信这应该是 1000,尽管将来可能会再次发生变化。它不是将不同的超时加在一起。

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.

吹泡泡o 2024-08-22 14:05:18

您可以使用 android.view.ViewConfiguration 中的 getLongPressTimeout 方法以编程方式确定此值。

有关详细信息,请参阅文档

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

See the docs for details.

空气里的味道 2024-08-22 14:05:18

一般来说,就像 Roman Nurik 提到的,您可以使用ViewConfiguration.getLongPressTimeout() 以编程方式获取长按值。默认值为 500 毫秒。

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

但是,可以通过将其设置为可访问性来全局自定义长按持续时间。值为短 (400 ms)、中 (1000 ms) 或长 (1500 ms)。您可以在 设置

// 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]);
    }
}

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]);
    }
}
自此以后,行同陌路 2024-08-22 14:05:18

这就是 R.array .long_press_timeout_selector_titles 看起来像:

    <!-- 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>

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>

栀梦 2024-08-22 14:05:18

嗯……我希望能得到累计时间。据我所知,getLongPressTimeout()是确定事件按下开始时添加的组件时间,加上TAP_TIMEOUT,加上???如果在网络浏览器中,则为 1000 毫秒。

我计算出它是 1650 毫秒,但我想对结果值进行一些确认。原因是我需要一些未与 SDK 集成的东西来预测长期持有。

我相信 getLongPressTimeout 的值是 500 毫秒,但手势显然需要更长的时间——接近 2 秒。

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.

于我来说 2024-08-22 14:05:18

View(及其大多数子类)使用 getLongPressTimeout。也许浏览器中的默认超时时间不够。

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

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