2012-03-17 146 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