2015-05-11 52 views
1

我有一個按鈕的主要活動,在點擊方法中,我正在創建一個DownloadManager類,它創建一個線程池以便從給定的URL下載視頻多線程。android:如何從用戶定義的類訪問UI對象

問題是,我如何將每個線程的狀態發佈到UI editText對象?

我在我的DownloadManager類中使用Handler來實例化線程並行運行。

以下爲點擊代碼在MainActivity按鈕:

public void buttonClick(View view) { 
    EditText eText1 = (EditText) findViewById(R.id.editText); //URL input text box 
    EditText eText = (EditText) findViewById(R.id.editText2); //output text box 
    new DownloadManager(eText1.getText().toString(), this.getFilesDir()).runDownloadThreadPool(); 

以下是下載管理器構造:

public DownloadManager(String url, File f) { 
    this.url1 = url; 
    this.f = f; 
    sDM = this; 
    mHandler = new Handler(Looper.getMainLooper()){ 
     @Override 
     public void handleMessage(Message inputMessage){ 
      DownloaderThreadPool t = (DownloaderThreadPool) inputMessage.obj; 
      //Toast.makeText(android.os.Environment.getApplicationContext(), t.threadMessage, Toast.LENGTH_LONG); 
     } 
    }; 
} 

如何通過ETEXT對象在第一個代碼的下載管理器,以便它可以在handleMessage覆蓋函數中更新?

回答

1

你可以通過構造函數傳遞:

public DownloadManager(String url, File f, EditText editText) { 
     this.url1 = url; 
     this.f = f; 
     sDM = this; 
     mHandler = new Handler(Looper.getMainLooper()){ 
      @Override 
      public void handleMessage(Message inputMessage){ 
       DownloaderThreadPool t = (DownloaderThreadPool) inputMessage.obj; 
       //Toast.makeText(android.os.Environment.getApplicationContext(), t.threadMessage, Toast.LENGTH_LONG); 

       editText.setText("Some text"); 
      } 
     }; 
    }

然後通過它,當你實例化你的下載管理器類:

new DownloadManager(eText1.getText().toString(), this.getFilesDir(), eText).runDownloadThreadPool();
+0

EditText參數在構造函數isnt final中,仍在工作... –

+0

@穆罕默德,好吧編輯答案,似乎我錯了。謝謝 ! – JonasCz

2

只需將其添加爲另一個參數構造函數。

public DownloadManager(EditText outputUI, String url, File f) { 
    this.outputUI = outputUI; 
    ... 
    mHandler = new Handler(Looper.getMainLooper()){ 
     @Override 
     public void handleMessage(Message inputMessage){ 
      DownloaderThreadPool t = (DownloaderThreadPool) inputMessage.obj; 
      outputUI.setText(t.threadMessage); 
     } 
    }; 
} 
0

1:通過構造函數將EditText的實例傳遞給DownloadManager,如上所述; 2:通過回調。具體如下:

首先,定義一個回調:

public interface OnStatusChangedListener { 

    public void onStatusChanged(int status); 

} 

,然後實現OnStatusChangedListener接口:

OnStatusChangedListener mListener = new OnStatusChangedListener() { 

    @Override 
    public void onStatusChanged(int status) { 
     //editText.setText(String.valueOf(status)); 
    } 
}; 

public DownloadManager(String url, File f, final OnStatusChangedListener listener) { 
    this.url1 = url; 
    this.f = f; 
    sDM = this; 
    mHandler = new Handler(Looper.getMainLooper()){ 
     @Override 
     public void handleMessage(Message inputMessage){ 
      DownloaderThreadPool t = (DownloaderThreadPool) inputMessage.obj; 
      //listener.onStatusChanged(status); 
     } 
    }; 
} 

最後,通過mListener在當你實例化你的下載管理器類:

new DownloadManager(eText1.getText().toString(), this.getFilesDir(), mListener).runDownloadThreadPool(); 
相關問題