2016-05-12 92 views
1

我在應用程序上實時更新佈局時遇到了一些麻煩。ANDROID:實時修改我的佈局

我想用「加載數據」按鈕顯示第一個屏幕。 當我按下按鈕時,我想將文本更改爲「加載...」,並在應用程序加載數據時顯示進度條。完成後,我想再次將文本更改爲「啓動應用程序」,並使進度條變得更糟糕。

我的問題是,在按鈕上的文本並不refrech和progressiv欄顯示不出來...

我已經嘗試了很多東西,有和沒有螺紋,在網上尋找幫助,但我找不到任何合適的提示。

也許你有想法幫助我? :d

public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    button=(Button)findViewById(R.id.button); 
    button.setOnClickListener(this); 
    button.setText("Load Data"); 
    findViewById(R.id.progressBar).setVisibility(View.INVISIBLE); 
} 

private void appInit() { 
      try { 
       ((MyApp) getApplication()).getDataFromServer(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      ((MyApp) getApplication()).initData(); 

} 


@Override 
public void onClick(final View v) { 
    Button b = (Button)v; 
    String buttonText = b.getText().toString(); 
    if(buttonText=="Load Data"){ 
     findViewById(R.id.progressBar).setVisibility(View.VISIBLE); 
     button.setText("Loading"); 
     appInit(); 
     while(!((MyApp)this.getApplication()).isInitSucceded()){} 
     button.setText("Launch App"); 
     findViewById(R.id.progressBar).setVisibility(View.INVISIBLE); 
     findViewById(R.id.button).setVisibility(View.VISIBLE); 
    } 
    else { 
     Intent intent = new Intent(MainActivity.this, ddcorp.test.Resume.class); 
     startActivity(intent); 
    } 
} 
+1

您需要了解[AsyncTasks](http://developer.android.com/reference/android/os/AsyncTask.html) - 它們旨在幫助您執行後臺任務(如appInit方法)並輕鬆更新UI。 – adelphus

+0

你可以在AsyncTask的幫助下做到這一點 - 例如http://stackoverflow.com/questions/16275913/show-progress-dialog-while-loading-data – hilzj

+0

2016,仍然使用AsyncTask很難過。去RxAndroid,它有點難以啓動,但是它比AsyncTask容易且功能強大得多。 – Leonardo

回答

0

阻止一個UI線程與下面一行是不是一個好主意:

while(!((MyApp)this.getApplication()).isInitSucceded()){} 

或者你可以使用的AsyncTask這對於後臺方法進程,進度更新和後期執行邏輯。這裏有一個例子:

public class MyTask extends AsyncTask<Void,Integer,String> 
{ 
    @override 
    public String doInBackground(Void... params) 
    { 
     //do your background processes 
     ... 
     publishProgress(someInteger); //for updating progress bar 
     ... 
     return result; 
    } 

    @override 
    public void onProgressUpdate(Integer... params) 
    { 
     //do your progress bar update with params[0] 
    } 

    @override 
    public void onPostExecute(String.. params) 
    { 
     //do something with the results of doInBackground stored in params[0] 
    } 
} 

,這裏是如何稱呼它:

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

謝謝你的提示! 它很棒! –

0

試試這個:

if(buttontext.equals("Load Data")){ 
    .. 
} 
+0

對不起,我忘了告訴我的問題在哪裏。事實上,文本只是沒有刷新,進度欄沒有顯示:/我的代碼是正確的,但我不知道爲什麼,佈局上的操作無效 –

0

這裏是我的解決方案,如果它可以幫助別人一天:d

Button button; 
ProgressBar progress; 
private Context mContext; 
MyApp mApplication; 
public Init(Context context){ 
    mContext = context; 
    mApplication = (MyApp) mContext.getApplicationContext(); 
} 


@Override 
protected void onPreExecute() { 
    ProgressBar progress=(ProgressBar)((Activity)mContext).findViewById(R.id.progressBar); 
    progress.setVisibility(View.VISIBLE); 
    button=(Button)((Activity)mContext).findViewById(R.id.button); 
    button.setText("Loading"); 
    button.setBackgroundColor(mContext.getResources().getColor(R.color.lgreen)); 
} 

@Override 
public Void doInBackground(Void... params) { 

    try { 
     mApplication.getDataFromServer(); 
     mApplication.initData(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 

    return null; 
} 


protected void onPostExecute(Void result) { 
    super.onPostExecute(result); 
    ProgressBar progress=(ProgressBar)((Activity)mContext).findViewById(R.id.progressBar); 
    progress.setVisibility(View.INVISIBLE); 
    button=(Button)((Activity)mContext).findViewById(R.id.button); 
    button.setText("Launch"); 
    button.setBackgroundColor(mContext.getResources().getColor(R.color.green)); 
    System.out.println("ok"); 
}