我正在嘗試爲我的手機編寫一個非常基本的應用程序,只是爲了讓自己回到編程的搖擺之中。我已經掌握了我想要做的框架和基礎知識,我只需要一小段幫助我的工作。Android - 動作進度對話框退出
在我的應用程序中,我有一個按鈕。當按下該按鈕時,我希望應用程序將一段文本更改爲「決定...」,彈出一個不確定的進度條,然後打印另一個活動的結果,但僅在對話框已經退出。目前我正在使用線程,但無法正常工作。
有沒有人有任何想法如何讓它按我想要的方式工作?
這是我的活動文件來源:
package com.bassios.decisionengine;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class DecisionEngineActivity extends Activity {
private ProgressDialog progressDialog;
public String decision;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void decide(View view) {
TextView tv = (TextView) findViewById(R.id.Decision1);
tv.setText("Deciding...");
int a = generate();
decision = decision(a);
runDialog(1);
// alterText();
}
public void alterText() {
TextView tv = (TextView) findViewById(R.id.Decision1);
tv.setText(decision);
}
private void runDialog(final int seconds) {
progressDialog = ProgressDialog.show(this, "Please wait...", "Deciding...");
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(seconds * 1000);
progressDialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
public static int generate() {
int a = (int) (Math.random() * 10) + 1;
return a;
}
public static String decision(int a) {
String decision = null;
switch (a) {
case 1:
decision = "Yes";
break;
case 2:
decision = "No";
break;
case 3:
decision = "Yes";
break;
case 4:
decision = "No";
break;
case 5:
decision = "Yes";
break;
case 6:
decision = "No";
break;
case 7:
decision = "Yes";
break;
case 8:
decision = "No";
break;
case 9:
decision = "Yes";
break;
case 10:
decision = "No";
break;
}
return decision;
}
}
謝謝!
嘗試使用'AsyncTask'而不是'Thread',你不能改變另一個'Thread'的UI狀態 –