如何在alertDialog中打开Progress Bar?

时间:2011-05-02 10:46:38

标签: android

我是android的新手。 我正面临一些麻烦 我的场景是 - 当我点击按钮所以打开alertDailog和alertdailog有两个按钮,如发送和取消,当我点击发送按钮,我想打开一个ProgressBar,因为发送按钮有havy内容,所以需要更多的时间。

我使用处理程序但没有找到任何确切的解决方案

我正在使用此代码

btn.setOnClickListener(new View.OnClickListener(){             public void onClick(查看v){

                handler.sendEmptyMessage(0);

        }

    });
  alert = new Dialog(ProgramDetailActivity.this);

        alert.setContentView(R.layout.dialog_email);
        alert.setTitle("                   Enter mail info");
        alert.setCancelable(true);
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,
                    int id) {
                finish();
            }
        };
             btnSend = (Button) alert.findViewById(R.id.btnsend);
         btnBack = (Button) alert.findViewById(R.id.btncancel);

        alert.show();

btnSend.setOnClickListener(new View.OnClickListener(){

            @SuppressWarnings("null")
                         public void onClick(View v) {
                        handler2.sendEmptyMessage(0);

//执行长时间操作                    progressDialog.dismiss();

                         toast=Toast.makeText(ProgramDetailActivity.this,"Mail has been sent Sucessfully!",

Toast.LENGTH_LONG); toast.show();

} }

private Handler handler2 = new Handler(){         public void handleMessage(Message msg){             alert.hide();             alert.cancel();             alert.dismiss();

progressDialog = ProgressDialog.show(v.getContext(),                                "Email Sending", "Please wait...");
    }
};

1 个答案:

答案 0 :(得分:1)

我还在Alertdialog的点击事件中使用了Progressdialog。代码如下:

AlertDialog.Builder submitalert = new AlertDialog.Builder(Summary.this);
 submitalert.setTitle(getResources().getString(R.string.app_name))
.setMessage("Submit the data to database?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener(){

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    update.setTitle(getResources().getString(R.string.app_name));
                    update.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    update.setCancelable(true);
                    update.setMax(100);
                    update.show();


                    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 (update.getProgress()<= update.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();
                        if(update.getProgress()== 100)
                           {
                               update.dismiss();
                           }
                    }




            })
            .setNegativeButton("No", new DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            })
            .show();