Android 中的 onLongPress 事件持续多长时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1930895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How long is the event onLongPress in the Android?
提问by mobibob
Android supports an event onLongPress. The question I have is 'how long' (in milliseconds) is the 'press' to trigger the event?
Android 支持 onLongPress 事件。我的问题是“按下”触发事件的“多长时间”(以毫秒为单位)?
回答by hackbod
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).
标准的长按时间是getLongPressTimeout()返回的时间,当前为 500 毫秒,但可能会发生变化(在 1.0 中为 1000 毫秒,但在以后的版本中发生了变化;也许将来它会是用户可自定义的)。
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.
浏览器使用自己的长按时间,因为它有一些更复杂的交互。我相信这应该是 1000,但它可能会在未来再次改变。它不是将不同的超时加在一起。
回答by Roman Nurik
回答by Viking Den
Generally, like Roman Nurik mentioned, you can use ViewConfiguration.getLongPressTimeout()to programmatically obtain long press value value. The default value is 500ms.
通常,就像 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;
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:
但是,长按持续时间可以通过将其设置为可访问性来全局定制。值为短 (400 毫秒)、中 (1000 毫秒) 或长 (1500 毫秒)。您可以在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]);
}
}
回答by Viking Den
This is what R.array.long_press_timeout_selector_titleslook like:
这是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>
回答by mobibob
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.
嗯......我希望得到累积时间。据我所知,getLongPressTimeout()是确定 event-press 开始时添加的组件时间,加上 TAP_TIMEOUT,加上 ??? 如果在网络浏览器中,则为 1000 毫秒。
I have calculated it to be 1650ms but I would like to have some confirmation of the resultantvalue. The reason is that I need something that is not integrated with the SDK to predict the long-hold.
我已经计算出它是 1650 毫秒,但我想对结果值进行一些确认。原因是我需要一些未与 SDK 集成的东西来预测长期持有。
I believe the value from getLongPressTimeout is 500ms, but the gesture clearly takes longer -- closer to 2 seconds.
我相信 getLongPressTimeout 的值是 500 毫秒,但手势显然需要更长的时间——接近 2 秒。
回答by James
View (and therefore most of its subclasses) uses getLongPressTimeout. Perhaps the default timeout was not sufficient in the browser.
View(及其大多数子类)使用 getLongPressTimeout。也许浏览器中的默认超时时间不够。