如何让我的应用在Android中等待?

时间:2014-06-11 18:15:09

标签: java android eclipse

在我的布局中,我的TextViews最初被设置为不可见。当用户按下开始按钮时,我希望其中一个TextView在.8秒内可见。然后我希望它再次变为不可见,并显示.8秒的另一个TextView,依此类推。基本上,它就像(textView1弹出一点然后消失,然后textView2弹出一点然后消失......)。每个textView之间需要有足够的时间。我的问题是找到一种方法让我的代码在:

之间等待一下
findViewById(R.id.textView1).setVisibility(00000000); //makes it visible

findViewById(R.id.textView1).setVisibility(00000004); //makes it invisible

我尝试了很多不同的方法,但是大多数方法都使textView保持不可见,因为它快速到达.setVisibility(00000004),或者暂停代码,但似乎永远不会像{{1}那样变得可见被忽略了。这就是我目前正在尝试的,但textViews保持不可见。

.setVisibility(00000000)

1 个答案:

答案 0 :(得分:0)

您可以尝试处理程序类 -

Handler handlerTimer = new Handler();
int count = 0;
Runnable runnable = new Runnable(){
    public void run() {
      if(count==0)
          findViewById(R.id.textView1).setVisibility(00000004);
      else if(count==1)
          findViewById(R.id.textView2).setVisibility(00000004);
      else if(count==2)
          findViewById(R.id.textView3).setVisibility(00000004);
      else if(count==3)
          findViewById(R.id.textView4).setVisibility(00000004);   
      count++;
      if(count<4)
      handler.postDealyed(runnable, 8000)   
  }};

或者您可以使用ScheduledThreadPoolExecutor

ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
int count=0;
scheduler.scheduleAtFixedRate
  (new Runnable() {
     public void run() {
        if(count==0)
          findViewById(R.id.textView1).setVisibility(00000004);
      else if(count==1)
          findViewById(R.id.textView2).setVisibility(00000004);
      else if(count==2)
          findViewById(R.id.textView3).setVisibility(00000004);
      else if(count==3)
          findViewById(R.id.textView4).setVisibility(00000004);   
      count++;
     }
  }, 0, 8, TimeUnit.SECONDS);

了解更多information