【问题标题】:Display Alert dialog box in just of progress dialog completed android在刚刚完成的进度对话框中显示警报对话框 android
【发布时间】:2014-03-24 11:02:40
【问题描述】:

你好朋友,我想在完成进度对话框中显示警告对话框。进度完成到 100 我想通过警报对话框询问/通知一些信息。我两者都很好,但我不知道如何整合两者,所以请快速帮助我..提前谢谢。 我的代码如下

                   lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            no = 1;
            int a = lv.getSelectedItemPosition();
            Toast.makeText(getApplicationContext(),
                    "item " + a + " selected",   Toast.LENGTH_SHORT).show();

            System.out.println("progress start");
            progressDoalog.setMax(100);
            progressDoalog.setMessage("Connecting Please Wait....");
            progressDoalog
                    .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDoalog.show();
            progressDoalog.setProgress(0);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {

                        // Here you should write your time consuming task...
                        while (progressDoalog.getProgress() <= progressDoalog
                                .getMax()) {

                            Thread.sleep(100);

                            handle.post(new Runnable() {

                                public void run() {
                                    progressDoalog.incrementProgressBy(no);
                                }

                            });

                            System.out.println("before if");
                            if (progressDoalog.getProgress() == progressDoalog
                                    .getMax()) {

                                System.out.println("u r in if 100");
                                progressDoalog.dismiss();
                                System.out.println("dismiss");
                                //
                                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                        context);

                                    alertDialogBuilder

                                        .setMessage("Password to Network")
                                        .setMessage(randomMessage)
                                        .setTitle("Password")
                                        .setCancelable(false)
                                        .setPositiveButton("Copy",new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,int id) {


                                            }
                                          })
                                        .setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,int id) {

                                                dialog.cancel();
                                            }
                                        });
                                    AlertDialog alertDialog = alertDialogBuilder.create();
                                    alertDialog.show();
                                //
                                System.out.println("alert over");

                            }
                        }
                    } catch (Exception e) {
                    }
                }
            }).start();


        }

    });

【问题讨论】:

    标签: android alert android-alertdialog progressdialog dialog


    【解决方案1】:

    您可以为此使用 AsyncTask

        public class MyAsyncTask extends AsyncTask<Void, Integer, Boolean>
        {
    
            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
        /** do your initialization here like setting up porgress bar or some other 
    
    variable it's up to you*/
            }
    
    
            @Override
            protected Boolean doInBackground(Void... params) {
                //do your work here 
                            //update progress bar 
                          publishProgress(total_progress);
                return flag;  //return true or false 
            }
    
            @Override
            protected void onProgressUpdate(Integer... values) {
    
                super.onProgressUpdate(values);
                         //set progress here
                         progressbar.setProgress(values[0])
            }
    
            @Override
            protected void onPostExecute(Boolean result) {
                      //deal with you alertdialog here
                if(result)
                         {
                          //do something with our alert dialog
                         }
                         else
                         {
                          //do something with your alert dialoge
                         }
                super.onPostExecute(result);
            }
    
    
    
    
        }
    

    【讨论】:

      【解决方案2】:

      通过处理程序将百分比值传递给异步任务来更新进度。完成后在 postExecute() 中显示警报对话框。

      【讨论】:

      • 请通过代码理解以上内容???现在我检查进度是否达到 100 我尝试显示警报,但它没有通过以下方式显示:if (progressDoalog.getProgress() == progressDoalog.getMax()) { my alert code }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多