2011-06-29 11 views
0

在處理註冊表單的函數中,我想將按鈕的可見性設置爲不可見,並將進度條的可見性設置爲可見。現在的問題是,處理一些OAuth請求的以下代碼似乎可以防止這些小部件在我想要的位置顯示其可見性。在setVisibility()之後等待,直到窗口小部件在應用中真實可見

直到oauth請求完成,設置進度條的可見性會停止(凍結?)。我該如何解決這個問題?我覺得跟sleep() S,wait() S或定時器工作會在這裏一個壞的解決方案......

private void registerOnce(){ 

    ProgressBar spinner = (ProgressBar) findViewById(R.id.progressBar); 
    Button login = (Button) findViewById(R.id.buttonLogin); 

    spinner.setVisibility(View.VISIBLE); 
    login.setVisibility(View.INVISIBLE); 

    // Here goes the code for some oauth requests 
    // This code here seems to prevent setting the 
    // the visibility where I want it to. 

    spinner.setVisibility(View.INVISIBLE); 
    login.setVisibility(View.VISIBLE); 

} 
+0

如果你的[R egistration方法可能在另一個線程中執行,您應該將'view.setVisibility()'方法的調用與主應用程序線程同步,但這是一個猜測 – wjeshak

回答

1

更好地使用AsyncTask
onPreExecute()

spinner.setVisibility(View.VISIBLE); 
    login.setVisibility(View.INVISIBLE); 

doInBackground

// Here goes the code for some oauth requests 
    // This code here seems to prevent setting the 
    // the visibility where I want it to. 

而且在onPostExecute

spinner.setVisibility(View.INVISIBLE); 
    login.setVisibility(View.VISIBLE); 
+0

Ok聽起來像是解決此問題的最佳方法。預執行和後執行似乎完全符合我的問題。我會試一試。非常感謝你。 – Marco

+0

完美的作品...; ) – Marco

0

單獨的UI從繁重的後臺進程。您的OATH請求與您的UI處於同一線程中。在另一個線程中更好地運行重要的操作(這裏是誓言連接)。或嘗試使用AsyncTask

相關問題