2014-10-28 47 views
1

我有三根弦在订单

String one="Connecting."; 
String two="Connecting.."; 
String three="Connecting..."; 

我有一个TextView多次更改文本视图的文本,我要的是设置文本在此以TextView的.. 一个 - > two-- >三→一→二→三等,直到过程完成。

我已经在基于价值做了它的循环,即

if(value%3==0){ 
          tx.setText(one); 
         } 
         else if(value%3==1){ 
          tx.setText(two); 
         } 
         else 
         { 
          tx.setText(three); 
         } 

This is done inside a thread and it is working well. 

,但我不想依靠“价值”。我想JST直到过程完成,以便文本更改提到以上。 我知道这是模糊的,但我没有得到如何做到这一点。 请如果有人可以给任何提示。

code: 


public void startProgress(View view) { 

     bar.setProgress(0); 
     tx.setText("Connect"); 
     new Thread(new Task()).start(); 
    } 

    class Task implements Runnable { 
     @Override 
     public void run() { 
      for (int i = 0; i <= 10; i++) { 
       final int value = i; 
       try { 
        Thread.sleep(500); 
        runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
          // TODO Auto-generated method stub 
          bar.setProgress(value); 

         String one="Connecting."; 
         String two="Connecting.."; 
         String three="Connecting..."; 


         if(value%3==0){ 
          tx.setText(one); 
         } 
         else if(value%3==1){ 
          tx.setText(two); 
         } 
         else 
         { 
          tx.setText(three); 
         } 


         } 
        }); 


       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 



      } 
     } 

    } 
+0

你使用计时器! – 2014-10-28 10:13:18

+0

请参阅编辑代码。我不使用计时器。 – 2014-10-28 10:17:08

+0

为什么你不使用进度条,看到这个例子http://android-er.blogspot.com/2011/07/display-indeterminate-progress-bar-on.html – 2014-10-28 10:32:23

回答

3

这是有道理的有一个像value不断增加的计数器。但是你可以做多一点简单:

String[] texts = new String[3] {"Connecting.", "Connecting..", "Connecting..."}; 
int value = 0; 

// ... 

tx.setText(texts[value]); 
value = (value+1)%3; 

这样,你不需要你的大凌乱if声明。你需要找到工作完成后通知你的线程的另一种方式,而不是只有固定次数的迭代。

+0

我编辑了我的代码。现在,如果你能看到它并建议我。 – 2014-10-28 10:18:09

+0

感谢它的工作。 – 2014-10-28 11:02:12

0

使用TextSwitcher代替TextView ...它会工作