Android:加载时更改进度对话框ProgressDialog.setMessage() [英] Android: Progress Dialog change ProgressDialog.setMessage() while loading
问题描述
我希望有人能帮助我找出如何在进度对话框中更改setmessage对话框的方法,而该对话框只有一个虚拟计时器,该计时器可以循环通过字符串数组或任何其他方式.例如,在加载时,它可能会显示正在加载...->建筑...->渲染...->等等.像一个1/2秒的计时器一样(这对我来说只是一个玩法,一旦我弄清楚如何更改对话框,我就可以使用实时更新来适应它.)我一直在使用
I was hoping someone could help me figure out how to change the setmessage dialog in a progress dialog with just a dummy timer that cycles through either an array of strings or any other way. For example While its loading it could say Loading... -> Building... -> Rendering... -> ect. on like a 1/2 sec timer (this is just for me play with it I can adapt it use real-time updating once I figure out how to change the dialog.) I've been using this tutorial for the progress thread minus the orientation code. Any ideas?
谢谢!
这不是很漂亮,但是我可以将其与之配合使用:
It's not pretty but I was able to get it to work with this:
public class BadBaconJoke extends Activity implements OnClickListener {
ProgressDialog dialog;
int increment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progressdialog);
Button startbtn = (Button) findViewById(R.id.startbtn);
startbtn.setOnClickListener(this);
}
public void onClick(View view) {
// get the increment value from the text box
EditText et = (EditText) findViewById(R.id.increment);
// convert the text value to a integer
increment = Integer.parseInt(et.getText().toString());
dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setTitle("Loading...");
dialog.setMessage("Almsot done!");
// set the progress to be horizontal
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// reset the bar to the default value of 0
dialog.setProgress(0);
// get the maximum value
EditText max = (EditText) findViewById(R.id.maximum);
// convert the text value to a integer
final int maximum = Integer.parseInt(max.getText().toString());
// set the maximum value
dialog.setMax(maximum);
// display the progressbar
dialog.show();
// create a thread for updating the progress bar
Thread background = new Thread (new Runnable() {
public void run() {
try {
// enter the code to be run while displaying the progressbar.
//
// This example is just going to increment the progress bar:
// So keep running until the progress value reaches maximum value
while (dialog.getProgress()<= dialog.getMax()) {
// wait 500ms between each update
Thread.sleep(500);
// active the update handler
progressHandler.sendMessage(progressHandler.obtainMessage());
}
} catch (java.lang.InterruptedException e) {
// if something fails do something smart
}
}
});
// start the background thread
background.start();
}
// handler for the background updating
Handler progressHandler = new Handler() {
public void handleMessage(Message msg) {
dialog.incrementProgressBy(increment);
int x;
x = dialog.getProgress();
switch (x) {
case 10: dialog.setMessage("Gathering Data"); break;
case 20: dialog.setMessage("Parsing Results"); break;
case 30: dialog.setMessage("Builindg Data"); break;
case 40: dialog.setMessage("TeraForming"); break;
case 50: dialog.setMessage("Contacting Server"); break;
case 60: dialog.setMessage("Ping Back"); break;
case 70: dialog.setMessage("Processing"); break;
case 80: dialog.setMessage("Communicating with Device"); break;
case 90: dialog.setMessage("Populating"); break;
case 100: dialog.setMessage("Displaying Data:"); break;
default: dialog.setMessage("..."); break;
}
if (x == 100){
new AlertDialog.Builder(BadBaconJoke.this);
dialog.setTitle("Complete");
//.setMessage("Are you sure you want to exit?")
// dialog.setButton(android.R.string.no, null);
//.setMessage("Are you sure you want to exit?")
}
}
};
}
有什么想法可以使它与微调器和过程对话框配合使用吗?当我更改dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);到dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
Any ideas how to get it work with a spinner vs a process dialog? When I change dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); to dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
它仅显示开关盒的默认设置.
it only displays the default from the switch case.
推荐答案
您可以使用onProgressUpdated()
来更改消息,因为此方法在UI线程上运行.但是,它不会给您机会在特定的时间范围内更改消息.
You can use onProgressUpdated()
in order to change the message, because this method is run on the UI Thread. Though, it doesn't give you the opportunity to change the message on the particular amounts of time.
这篇关于Android:加载时更改进度对话框ProgressDialog.setMessage()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!