Android计时器和UI线程

时间:2012-12-12 05:47:16

标签: android timer inflate

我像这样膨胀一个TableRow:

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();

问题是,当我运行此计时器并将其显示在行中时,UI非常慢并且计时器滞后。当我在onTouchListener()中执行view.setBackGroundColor(Color.BLACK)并单击它时,它会滞后。

1 个答案:

答案 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
     }
    }