2012-03-17 145 views
0

我想寫一個實時計數器。我試圖使用線程,但是android警告我只有活動線程可能觸及視圖。我發現了一個解決方案與runOnUiThread,但它不工作太runOnUiThread,更改TextView

public class CounterInRealTimeExampleActivity extends Activity { 
     private TextView textView; 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      LinearLayout layout = new LinearLayout(this); 
      textView = new TextView(this); 
      textView.setTag(new Integer(0)); 

      layout.addView(textView); 
      setContentView(layout); 
      runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        //while(true) 
        { 
         try { 
          Thread.sleep(3000); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         increment(); 
        } 
       } 
      }); 
     } 
     public void increment() { 
      textView.setTag(new Integer((Integer)textView.getTag())+1); 
      textView.setText(Integer.toString((Integer)textView.getTag())); 
     } 
    } 
+0

你嘗試http://developer.android.com/reference/android/os/CountDownTimer.html? – Calvin 2012-03-17 13:43:42

回答

4

我這裏是粘貼代碼。這可能幫助你..

class UpdateTimeTask extends TimerTask {  

    public void run() { 

     String time = DateUtils.now("hh:mm:ss'T'a");    
     String[]arrValues = time.split("T"); 
     if(arrValues.length>0) { 
      String strValue= arrValues[0]; 
      String []arrTimeValues = strValue.split(":"); 
      String strValue1= arrTimeValues[2]; 
      setTimertext(strValue1);     
     }      
    } 

    public void setTimertext(String strValue) {   
     runOnUiThread(new Runnable() { 
      public void run() { 
       FinalTime=timer_time--;     
       btnTimer.setText(String.valueOf(FinalTime));  
      } 
     }); 
    } 

} 
+1

非常感謝(我不能投票,由於我的名譽低,但我會;) – test30 2012-03-17 13:51:53

+1

okey沒有問題,聲譽並不重要,但我真的很高興,如果這個解決方案是幫助你。謝謝 – Hasmukh 2014-03-20 05:55:02

3
Thread.sleep(3000); 

肯定是有問題。您不允許阻止UI線程,這正是sleep所做的。您必須執行代碼以從runOnUiThread完全更新UI,僅限於

2

工作之一,Hasmukh舉報後發現解決方案

package com.android.examples; 

import java.util.Calendar; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class CounterInRealTimeExampleActivity extends Activity { 
    private TextView textView; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     LinearLayout layout = new LinearLayout(this); 
     textView = new TextView(this); 
     layout.addView(textView); 
     setContentView(layout); 

     new Thread(new Runnable() { 

      @Override 
      public void run() { 
       while (true) { 

        updateTime(); 
        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 

      } 
     }).start(); 
    } 

    private void updateTime() { 
     runOnUiThread(new Runnable() { 

      @Override 
      public void run() { 
       textView.setText(Integer.toString((int) (Calendar.getInstance() 
         .getTimeInMillis()/1000) % 60)); 
      } 
     }); 
    } 
} 
+0

這是很好的親愛的.. – Hasmukh 2012-03-19 05:06:59