2016-08-24 38 views
-1

如何設置一個計時器TextView?此代碼總是崩潰我的應用程序如何爲textView設置計時器?這不斷崩潰我的應用程序

final TextView textwel = (TextView) findViewById(R.id.welcometext); 
Thread timer = new Thread() { 
    public void run() { 
     try { 
      // this should sleep for 4 seconds 
      sleep(4000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 

     textwel.setText("Welcome"); 
    } 
}; 

啓動定時器

timer.start(); 
+0

那是一個不錯的辦法的人。因爲它會產生一個空指針異常,當視圖被破壞時,後臺線程已經調用視圖已經很晚了。 – Enzokie

+0

爲此使用處理程序。 – Enzokie

回答

0

這是行不通的。

由於您創建的新線程不是UI線程,因此無法更新該線程上的UI(設置文本視圖的文本)。

更好的方法是使用android.os.Handler類。它有一個postDelayed方法將延遲執行RunnableHere是文檔。

或者,您也可以使用這個Timer類我寫的,它封裝了一個Handler實例,有一個簡單的界面:

import android.os.Handler; 

public class Timer { 
    private Handler handler; 
    private boolean paused; 

    private int interval; 

    private Runnable task = new Runnable() { 
     @Override 
     public void run() { 
      if (!paused) { 
       runnable.run(); 
       Timer.this.handler.postDelayed (this, interval); 
      } 
     } 
    }; 

    private Runnable runnable; 

    public int getInterval() { 
     return interval; 
    } 

    public void setInterval(int interval) { 
     this.interval = interval; 
    } 

    public void startTimer() { 
     paused = false; 
     handler.postDelayed (task, interval); 
    } 

    public void stopTimer() { 
     paused = true; 
    } 

    public Timer (Runnable runnable, int interval, boolean started) { 
     handler = new Handler(); 
     this.runnable = runnable; 
     this.interval = interval; 
     if (started) 
      startTimer(); 
    } 
} 

Gist

你可能會使用這樣的:

Runnable run = new Runnable() { 
    public void run() { 
     textwel.setText("Welcome"); 
    } 
}; 
Timer timer = new Timer(run, 4000, true); 

PS每4秒將textview的文本設置爲「歡迎」是毫無意義的。

-1

試試這個,它的工作100%,而且UI也不會卡住

Thread timer = new Thread() { 
    public void run() { 
     try { 
      // this should sleep for 4 seconds 
      sleep(4000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
// after sleep call a UI thread 
     runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
          textwel.setText("Welcome"); 
         } 
        }); 
    } 
}; 
0

您可以使用CountDownTimer API更新的TextView計時器,這是非常簡單易用,方便。

下面是簡單的例子:

TextView tvTime = (TextView)findViewById(R.id.tv_time); 
CountDownTimer countDownTimer = new CountDownTimer((durationInMilis * 60000), 1000) { 
     @Override 
     public void onTick(long millisUntilFinished) 
     { 
      String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millisUntilFinished), 
        TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)), 
        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))); 
      tvTime.setText(hms); 
     } 

     @Override 
     public void onFinish() 
     { 
      // Do your other task on finish 
     } 
    }.start(); 

此代碼,這將更新tvTime文字每秒,並運行在durationInMilis時間sepecified量。輸出看起來像hh:mm:ss例如01:23:57

如果你不想在更新上顯示任何內容,請將onTick()回調留空並更新onFinish()中的TextView。只需將值傳遞給您想要爲其運行計時器的參數durationInMilis。

如果您有任何疑問,請隨時發表評論。

0

你爲什麼要用textview?

天文臺是由android提供的一個類,實現了一個簡單的計時器,所以我的建議是用它來代替textview。

這裏是official link

0

//使用它,它很容易和簡單的

new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 

      textwel.setText("Welcome"); 
     } 

    }, 4000); 
相關問題