2013-06-24 53 views
1

我有一個按鈕,當我點擊它時,我使textView.setText()成爲一個非常大的文本,50000個符號,並且界面停止3秒,我該如何修復它?TextView中的長文本

  1. 我試圖使它與句柄和線程,但它沒有幫助。
  2. 我試過使textview.append(),但它也沒有幫助。

      MainActivity.this.runOnUiThread(new Runnable() { 
    
          @Override 
          public void run() { 
    
           textText.append(rowItemHeader.getText().substring((endOfNews - 10000, endOfNews)); 
          } 
         }); 
    

    編輯1無結果

private class MyTask extends AsyncTask <Void, Void, String> { String str; TextView txt;

MyTask(String str, TextView txt){ 
     this.str = str; 
     this.txt = txt; 

    } 
    public String doInBackground(Void... args) { 

     return str.substring(endOfNews - 10000, endOfNews); 
    } 

    public void onPostExecute(String myString) { 
     // do your update here or you will get that error (the original thread one) 
     txt.append(myString); 
    } 
} 
+0

什麼不起作用?變量myString是否正確闡述? – Enrichman

+0

它的工作,但我已經延遲了1-3秒,當我點擊按鈕。如果我有小文本,它的工作速度很快,但如果我有非常大的文本,我有延遲。 –

+0

好的,但你有沒有解決這個與AsyncTask? – Enrichman

回答

0

你怎麼聲明線程? Yu've使用類似的東西:

this.runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      Log.e("TAG synchronize", "synchronize"); 
     } 
}); 
+0

我這樣做,但它也停止接口1-3秒:( –

0

使用AsyncTask,他們都是爲了你想要什麼。它們是一個輔助線程,可以很容易地從主線程中設置和執行。

使用,使用runOnUiThread(new Runnable(...)...),並呼籲Thread.sleep(50)迫使線程的那麼多毫秒,以防止滯後睡覺生成的文本到TextViewappends部分線程內循環。

+0

如果我與AsyncTask我有錯誤「只有原始創建一個視圖層次結構的線程可以觸及其視圖。「 –

+0

然後你沒有完全做到這一點,你沒有做'runOnUiThread()'使它」在ui線程上運行「!只有'appending'應該是「在ui線程上運行」不要把整個'AsyncTask'操作放在ui上運行,因爲這樣做會凍結它。 – LuckyMe

0

使用的東西,如一個AyncTask:

private class MyTask extends AsyncTask<Void, Void, String> { 

    private Context context;  

    public MyTask(Context context) { 
     this.context = context; 
    } 

    public String doInBackground(Void... args) { 
     // do your job here 
     return myString; 
    } 

    public void onPostExecute(String myString) { 
     // do your update here or you will get that error (the original thread one) 
     ((TextView)context.findViewById(R.id.textview)).setText(myString); 
    } 
} 

,然後在活動

new MyTask(MyActivity.this).execute(); 
+0

沒有結果,我編輯我的問題:( –

0

首先,刪除視圖,然後添加在後臺線程的文本,然後添加視圖回來。告訴我你是否需要一些示例代碼。

+0

嗯,有趣,你能給一些示例代碼 –