[英]how can we increase button long click listener time in android?
i want to execute simple click listener as well as long click listener on same button. 我想在同一按钮上执行简单的单击侦听器以及长单击侦听器。 But i need to execute long click listener after 5 seconds delay in the longclicklistener executes after 1 seconds of hold .so using handler it will executes after 5 seocnds.but i need exact to press button for 5 seconds then code to executes ...
但是我需要在longclicklistener延迟1秒后执行5秒延迟后执行long click侦听器。因此使用处理程序将在5秒后执行。但是我需要精确按下按钮5秒钟然后执行代码...
It is not possible to change the timer on the onLongClick event, it is managed by android itself. 无法更改onLongClick事件上的计时器,它由android本身管理。
What is possible is to use .setOnTouchListener(). 可以使用.setOnTouchListener()。
Then register when the MotionEvent is a ACTION_DOWN. 然后在MotionEvent为ACTION_DOWN时注册。 Note the current time in a variable.
注意变量中的当前时间。 Then when a MotionEvent with ACTION_UP is registered and the current_time - actionDown time > 5000 ms then do something.
然后,当注册带有ACTION_UP的MotionEvent且current_time-actionDown时间> 5000 ms时,请执行以下操作。
so pretty much: 差不多了:
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) > 5000){
// 5 second of long click
return true;
}
}
return false;
}
})
you can use Handler
like this way: 您可以这样使用
Handler
:
Button b=findViewById(R.id.btn);
final Runnable run = new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
// Your code to run on long click
}
};
final Handler handel = new Handler();
b.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
handel.postDelayed(run, 5000/* OR the amount of time you want */);
break;
default:
handel.removeCallbacks(run);
break;
}
return true;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.