2016-06-13 56 views
-2

我已經嘗試過使用線程,但它不成功,使用EditText作爲輸入改變TextView文本後,textView沒有改變。請幫幫我!如何在Android中持久更改TextView文本?

 Thread thread = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      //shared is SharedPreferences object that I define as an instance variable 
      String inp = shared.getString("input", def); 

      textView.setText(inp); 
      Log.d("input",inp); 
    }); 

    thread.start(); 
+0

你能解釋一下你想要做什麼? –

回答

0

試試這個:

Thread thread = new Thread(new Runnable() { 
    @Override 
    public void run() { 
     //shared is SharedPreferences object that I define as an instance variable 
     String inp = shared.getString("input", def); 
     runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 

         textView.setText(inp); 
         Log.d("input",inp); 
        } 
       }); 
}); 

thread.start(); 

更新UI,你應該使用主UI線程。看一看:

Android runOnUiThread explanation

+0

哦,好吧,我得到它。謝謝。 –

0

你爲什麼要在一個單獨的線程中執行它。即使你想,你也不能在非UI線程中更新任何UI組件,例如textView。

相關問題