问题描述
我已经用OnTouchListener可见布局1,而屏幕是触摸。现在我想无形的布局1,而在屏幕没有被触摸三秒钟。但我不知道我可以使用哪些事件侦听器?
现在的问题已得到解决。但是,另外一个是出现。我使用的:
类unTouchTask扩展的TimerTask {
公共无效的run(){
如果(untouch ==真){
RelativeLayout的RL =(
RelativeLayout的)findViewById(R.id.relativeLayout2);
rl.setVisibility(View.INVISIBLE);
timer.cancel();
untouch = FALSE;}
}
}
下面就行误差 rl.setVisibility(View.INVISIBLE);
:
android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程。
选项一:你需要运行一个计时器来跟踪用户intraction活动时间,并在每个用户的触摸,您将需要复位定时器
。code你的计时器如下:
类UpdateTimeTask扩展的TimerTask {
公共无效的run(){
//隐藏布局
}
}
而在事件监听器来启动此更新,以下定时器()实例用于:
如果(startTime时== 0L){
的startTime = evt.getWhen();
定时器=新的Timer();
timer.schedule(新UpdateTimeTask(),300,200);
}
注:特别要注意的300,200个参数。第一个参数是指等待300毫秒运行时钟更新任务第一次了。第二个手段重复每200ms后,直到停止。
选项2:幸运的是,定时器的作用,可以通过android.os.Handler类来代替,用了一些调整
您可以得到更详细的例子在 http://www.vogella.de/articles /AndroidPerformance/article.html
问候。
I has use OnTouchListener to visible layout1 while screen was touch.Now I want invisible layout1 while the screen didn't be touch three seconds.But I don't know which event listener can I use?
Now the problem has be resolved.But another one was appear.I use:
class unTouchTask extends TimerTask {
public void run() {
if(untouch == true) {
RelativeLayout rl = (
RelativeLayout)findViewById(R.id.relativeLayout2);
rl.setVisibility(View.INVISIBLE);
timer.cancel();
untouch = false;}
}
}
Below error on linerl.setVisibility(View.INVISIBLE);
:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
Option1 : You need to run a timer to track the user intraction inactive time, and on every user touch you will need to reset the timer.
Code your timer as below:
class UpdateTimeTask extends TimerTask {
public void run() {
//hide your layout
}
}
And in the event listener to start this update, the following Timer() instance is used:
if(startTime == 0L) {
startTime = evt.getWhen();
timer = new Timer();
timer.schedule(new UpdateTimeTask(), 300, 200);
}
NOTE :In particular, note the 300, 200 parameters. The first parameter means wait 300 ms before running the clock update task the first time. The second means repeat every 200ms after that, until stopped.
Option 2: Fortunately, the role of Timer can be replaced by the android.os.Handler class, with a few tweaks
You can get more detailed example at http://www.vogella.de/articles/AndroidPerformance/article.html
Regards.
这篇关于如何隐藏布局时,屏幕上的用户保持不变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!