英文:
Performing different tasks by holding text view using touch listener
问题
代码:
textview_temp2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i("TAG1", "按下");
// 在这里开始计时器
return true;
case MotionEvent.ACTION_UP:
Log.i("TAG3", "抬起");
// 在这里停止计时器
return true;
}
return false;
}
});
现在,如果用户长按文本视图超过5秒,我希望调用事件动作“抬起”。并且只有这两个事件中的一个应该被调用。
英文:
I have a text view, on which I am implementing the touch listener.
Code:
textview_temp2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.i("TAG1","touched Down");
return true;
case MotionEvent.ACTION_UP:
Log.i("TAG3","touched UP");
return true;
}
return false;
}
});
Now, I want to call event action up if the user holds the text view for 5 or more seconds. Also, only one among these two events should be called.
答案1
得分: 1
触摸按下时保存系统时间,然后在触摸抬起时比较经过了多少时间。如果经过了3-5秒之间的时间,执行3秒的动作;如果经过的时间超过5秒,执行5秒的动作。如果你想在经过5秒时自动执行5秒的动作,即使用户一直按住触摸,你可以使用一个持续5秒的计时器,在初始触摸按下时启动计时器,在触摸抬起时停止计时器。如果计时器倒数到0而没有被触摸抬起停止,计时器应调用你想要执行的同一方法/动作,以进行5秒的操作。
英文:
Save the system time when touched down, then at touched up compare how much time elapsed. Anything between 3-5 seconds you execute the 3 second action, anything above the 5 second you execute the 5 seconds action. If you want to automatically do the 5 second action at 5 seconds even if the user keep the touch down, then you could use a timer with a 5 seconds duration, you start the timer on initial touch down and stop it on touched up. The timer should call the same method/action you want for the 5 seconds if it ticks down to 0 without being stopped by a touched up.
答案2
得分: 0
容易完成。您需要使用处理程序来处理延迟的任务。
final int STEP3SECS = 3;
final int STEP5SECS = 5;
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == STEP3SECS) {
// 进行3秒的任务
} else if (msg.what == STEP5SECS) {
// 进行5秒的任务
}
return false;
}
});
然后:
textview_temp2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.i("TAG1","按下");
handler.sendEmptyMessageDelayed(STEP3SECS,3000);
handler.sendEmptyMessageDelayed(STEP5SECS,5000);
return true;
case MotionEvent.ACTION_UP:
Log.i("TAG3","抬起");
handler.removeCallbacksAndMessages(null);
return true;
}
return false;
}
});
ACTION_UP
可以在3个不同的时间间隔内发生:在3秒之前,没有任务被执行。在3秒之后和5秒之前,只有3秒的任务会被执行。在5秒之后,两个任务都会被处理。
英文:
Easy to accomplish. You need to use a handler to handle your delayed tasks.
final int STEP3SECS = 3;
final int STEP5SECS = 5;
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == STEP3SECS) {
//do 3 second job
} else if (msg.what == STEP5SECS) {
//do 5 second job
}
return false;
}
});
And then:
textview_temp2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.i("TAG1","touched Down");
handler.sendEmptyMessageDelayed(STEP3SECS,3000);
handler.sendEmptyMessageDelayed(STEP5SECS,5000);
return true;
case MotionEvent.ACTION_UP:
Log.i("TAG3","touched UP");
handler.removeCallbacksAndMessages(null);
return true;
}
return false;
}
});
ACTION_UP
can take place in 3 different time intervals: before 3 seconds, well no job gets done. After 3 seconds and before 5 seconds, only 3 second job gets done. And after 5 seconds which both jobs are handled.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论