Android:ProgressDialog加载时更改ProgressDialog.setMessage()

Android:ProgressDialog加载时更改ProgressDialog.setMessage(),android,multithreading,process,dialog,Android,Multithreading,Process,Dialog,我希望有人能帮我弄清楚如何在进程对话框中更改setmessage对话框,只需一个虚拟计时器,它可以通过字符串数组或任何其他方式循环。例如,当它加载时,它可以说加载…->建筑…->呈现…->发射型计算机断层扫描仪。就像一个1/2秒的计时器(这只是为了我玩它,我可以调整它使用实时更新,一旦我弄清楚如何更改对话框。)我一直在使用进度线程减去方向代码。有什么想法吗 谢谢 虽然不太好看,但我还是能让它工作: public class BadBaconJoke extends Activity implem

我希望有人能帮我弄清楚如何在进程对话框中更改setmessage对话框,只需一个虚拟计时器,它可以通过字符串数组或任何其他方式循环。例如,当它加载时,它可以说加载…->建筑…->呈现…->发射型计算机断层扫描仪。就像一个1/2秒的计时器(这只是为了我玩它,我可以调整它使用实时更新,一旦我弄清楚如何更改对话框。)我一直在使用进度线程减去方向代码。有什么想法吗

谢谢

虽然不太好看,但我还是能让它工作:

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?")
         }


        }
    };










}
公共类BadBaconJoke扩展活动实现OnClickListener{
进程对话;
整数增量;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.progressdialog);
按钮startbtn=(按钮)findViewById(R.id.startbtn);
startbtn.setOnClickListener(此);
}
public void onClick(视图){
//从文本框中获取增量值
EditText et=(EditText)findViewById(R.id.increment);
//将文本值转换为整数
increment=Integer.parseInt(et.getText().toString());
dialog=新建ProgressDialog(此);
对话框。可设置可取消(true);
对话框.setTitle(“加载…”);
setMessage(“Almsot完成!”);
//将进度设置为水平
设置ProgressStyle(ProgressDialog.STYLE_水平);
//将条形图重置为默认值0
对话框。设置进度(0);
//获取最大值
EditText max=(EditText)findViewById(R.id.max);
//将文本值转换为整数
final int max=Integer.parseInt(max.getText().toString());
//设置最大值
dialog.setMax(最大值);
//显示进度条
dialog.show();
//创建用于更新进度条的线程
线程背景=新线程(newrunnable(){
公开募捐{
试一试{
//输入显示进度条时要运行的代码。
//
//本例只是增加进度条:
//因此,继续运行,直到进度值达到最大值

while(dialog.getProgress()您可以使用
onProgressUpdated()
来更改消息,因为此方法是在UI线程上运行的。但是,它没有给您在特定时间段更改消息的机会