Android的 - OnTouch做工作 [英] Android - OnTouch Do Work
问题描述
我有一些code已经让我非常接近了什么我试着去实现。但也有一些问题。我想要做的是,当按钮被pssed并举行$ P $,它会不断地调用一个方法AdvanceLog()。问题是,在ACTION_DOWN运动事件不会继续被调用。会发生什么事是,当你preSS不放,第一运动ACTION_DOWN和每一个动作之后,在继续按住是ACTION_MOVE。
这会引起一些问题,因为我没有真正寻找的移动操作,更使刚的repetetive下来的动作。有什么建议?
advanceButton.setOnTouchListener(新View.OnTouchListener(){ @覆盖
公共布尔onTouch(视图V,MotionEvent事件)
{
INT计数器= 0;
如果(event.getAction()== MotionEvent.ACTION_DOWN || event.getAction()== MotionEvent.ACTION_MOVE)
{
fwdTouchCounter ++;
AdvanceLog(真实,真实);
尝试
{
如果(fwdTouchCounter→5)
视频下载(30);
其他
视频下载(800);
}
赶上(InterruptedException的E)
{ e.printStackTrace();
}
}
否则如果(event.getAction()== MotionEvent.ACTION_UP)
fwdTouchCounter = 0; 返回false; }
});
在你的 ACTION_DOWN
触摸事件,断火,调用本地方法的线程。在此方法中不断地读取类变量(布尔
是最简单的),如果该变量,我们称之为 m_advanceLog
是真实的,继续处理(进你的日志我猜)。在你的 ACTION_UP
触摸事件,设置 m_advanceLog
为false,应停止循环,并允许线程退出。
请记住,这仅仅是一个好主意,如果没有用户界面的变化,othwerwise一些额外的工作需要,由于线程交互。
I have some code that has got me very close to what Im trying to achieve. But there are a few issues. What I want to do is, when a Button is pressed and held, it will continuously call a method AdvanceLog(). The problem is, the ACTION_DOWN motion event does not continue to get called. What happens is, when you press and hold, the first motion is ACTION_DOWN and every motion thereafter while still holding down is ACTION_MOVE. This is causing some issues, as I am not REALLY looking for the move action, more so just the repetetive down action. Any suggestions?
advanceButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event)
{
int counter=0;
if(event.getAction()==MotionEvent.ACTION_DOWN || event.getAction()==MotionEvent.ACTION_MOVE)
{
fwdTouchCounter++;
AdvanceLog(true, true);
try
{
if(fwdTouchCounter>5)
Thread.sleep(30);
else
Thread.sleep(800);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
else if(event.getAction()==MotionEvent.ACTION_UP)
fwdTouchCounter=0;
return false;
}
});
On your ACTION_DOWN
touch event, fire off a thread that calls a local method. Inside this method constantly read a class variable (boolean
is easiest) and if that variable, let's call it m_advanceLog
is true, continue to process (advancing your log I'm guessing). In your ACTION_UP
touch event, set m_advanceLog
to false which should stop your loop and allow the thread to exit.
Keep in mind that this is only a good idea if there aren't user interface changes, othwerwise some additional work is needed due to the thread interaction.
这篇关于Android的 - OnTouch做工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!