gpt4 book ai didi

Android:进度对话框在加载时更改 ProgressDialog.setMessage()

转载 作者:太空狗 更新时间:2023-10-29 12:56:19 25 4
gpt4 key购买 nike

我希望有人能帮我弄清楚如何仅使用一个循环遍历字符串数组或任何其他方式的虚拟计时器来更改进度对话框中的设置消息对话框。例如,在加载时它可以说正在加载... -> 建筑... -> 渲染... -> 等等。就像一个 1/2 秒的计时器(这只是为了我玩它,一旦我弄清楚如何更改对话框,我可以使用实时更新来调整它。)我一直在使用 this tutorial对于进度线程减去方向代码。有什么想法吗?

谢谢!

它不是很漂亮,但我能够让它与这个一起工作:

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);

它只显示 switch case 的默认值。

最佳答案

您可以使用 onProgressUpdated() 来更改消息,因为此方法在 UI 线程上运行。但是,它不会让您有机会在特定时间段内更改消息。

关于Android:进度对话框在加载时更改 ProgressDialog.setMessage(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6454419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com