問題描述
在我正在處理的應用程序中,我要求用戶必須單擊 &在某個動作發生之前持有一個組件一段時間.
In an application I'm working on, I have the requirement that a user must click & hold a component for a period time before a certain action occurs.
我目前正在使用OnLongClickListener來監聽longclick,但是我發現觸發OnLongClick事件的點擊時長太短了.
I'm currently using an OnLongClickListener to listen for the longclick, but I find that the length of a click to trigger the OnLongClick event is too short.
例如,假設 LongClick 事件在單擊 400 毫秒后觸發,但我希望用戶必須單擊 &在事件觸發前保持 1200ms.
For example, let's say the LongClick event triggers after a 400ms click, but I want the user to have to click & hold for 1200ms before the event triggers.
有什么方法可以將 LongClick 事件配置為需要更長的點擊時間?
或者是否有其他構造可以讓我聽到更長的點擊?
Is there any way I can configure the LongClick event to require a longer click?
Or is there perhaps another construct that would allow me to listen for longer clicks?
推薦答案
onLongClick事件不能改變定時器,由android自己管理.
It is not possible to change the timer on the onLongClick event, it is managed by android itself.
可以使用 .setOnTouchListener().
What is possible is to use .setOnTouchListener().
然后在 MotionEvent 為 ACTION_DOWN 時注冊.
請注意變量中的當前時間.
然后當一個帶有 ACTION_UP 的 MotionEvent 被注冊并且 current_time - actionDown 時間 > 1200 毫秒時,然后做一些事情.
Then register when the MotionEvent is a ACTION_DOWN.
Note the current time in a variable.
Then when a MotionEvent with ACTION_UP is registered and the current_time - actionDown time > 1200 ms then do something.
差不多:
Button button = new Button();
long then = 0;
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
then = (Long) System.currentTimeMillis();
}
else if(event.getAction() == MotionEvent.ACTION_UP){
if(((Long) System.currentTimeMillis() - then) > 1200){
return true;
}
}
return false;
}
})
這篇關于LongClick 事件發生得太快.如何增加觸發它所需的點擊時間?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!