2012-12-12 29 views
1

我吹一個的TableRow是這樣的:Android的定時器和UI線程

final TableRow tr1 = (TableRow)LayoutInflater.from(this).inflate(R.layout.attrib_row_survey, null); 
tr1.setOnTouchListener(this); 
tl1.addView(tr1); 

這是我做我的櫃檯:

new CountDownTimer(30000, 1000) { 

       public void onTick(long millisUntilFinished) { 
        ((TextView)tr1.findViewById(R.id.textView2)).setText("Seconds Remaining: " + millisUntilFinished/1000); 
       } 

       public void onFinish() { 
        ((TextView)tr1.findViewById(R.id.textView2)).setText("DONE"); 
       } 
       }.start(); 

的問題是,當我運行這個計時器,並顯示在該行,用戶界面非常慢,計時器滯後。當我在onTouchListener()中做一個view.setBackGroundColor(Color.BLACK)時,我點擊它,它滯後。

+0

一個改進是保存對'textView2'的引用,而不是重複調用findViewById()。然而,CountDownTimer不可能產生明顯的滯後,你還在做什麼? – Sam

+0

我可以在我的答案感興趣嗎[這裏](http://stackoverflow.com/questions/3733867/stop-watch-logic) – st0le

+0

肯定繼續... –

回答

0
protected static void startTimer() { 
    isTimerRunning = true; 
    timer.scheduleAtFixedRate(new TimerTask() { 
     public void run() { 
      elapsedTime += 1; //increase every sec 
      mHandler.obtainMessage(1).sendToTarget(); 

     } 
    }, 0, 1000); 
} 

    public Handler mHandler = new Handler() { 

    public void handleMessage(Message msg) { 
      StopWatch.time.setText(formatIntoHHMMSS(elapsedTime)); //this is the textview 
    } 
    } 
+0

我如何格式化它,使它倒數而不是向上? –

+1

來自commonsware的好答案http://stackoverflow.com/a/2691510/603233 –

+0

那麼,如果TableRow onTouchListener被激活,我應該如何停止執行onTick的倒計時器?每次我觸摸TableRow時,它都會滯後。 –