2014-10-28 48 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 ...它會工作