我在應用程序上實時更新佈局時遇到了一些麻煩。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);
}
}
您需要了解[AsyncTasks](http://developer.android.com/reference/android/os/AsyncTask.html) - 它們旨在幫助您執行後臺任務(如appInit方法)並輕鬆更新UI。 – adelphus
你可以在AsyncTask的幫助下做到這一點 - 例如http://stackoverflow.com/questions/16275913/show-progress-dialog-while-loading-data – hilzj
2016,仍然使用AsyncTask很難過。去RxAndroid,它有點難以啓動,但是它比AsyncTask容易且功能強大得多。 – Leonardo