【问题标题】:Code after ProgressDialog (Android) gets executed before the ProgressDialog's timer runs outProgressDialog (Android) 之后的代码在 ProgressDialog 的计时器用完之前执行
【发布时间】:2016-11-29 16:14:51
【问题描述】:

当我单击一个按钮BUTTON 时,我会显示一个 ProgressDialog 弹出窗口,其中显示了圆形的进度表。现在应该运行 N 秒,然后,有一些代码仅在临时创建的 ProgressDialog 实例消失后(当 N 秒结束时)执行。代码在 TextView 中显示了一些文本,每次点击 BUTTON 时文本都会发生变化,并且图像也会发生变化。但是只有在 ProgressDialog 结束后才能看到更改

但问题是,当我点击BUTTON 时,文本已经改变,ProgressDialog 循环对话框播放 N 秒,这是不应该发生的 - 只有在 ProgressDialog 完成循环后,文本才会改变在 N 秒内,应用程序 View 重新获得焦点,然后文本被更改。

代码如下:

BUTTON.setOnClickListener(new View.OnClickListener() { 

            public void onClick(View v) {
                // TODO Auto-generated method stub

    imageView.setVisibility(ImageView.INVISIBLE);

    //Choosing MESXs type to show =================================
                if(mTSFlag==1)
                    mTS=holA;
                else
                    mTS=holB;

    // ProgressDialog =================================
                final ProgressDialog progressRing=ProgressDialog.show(MainActivity.this, "Please wait..", "This takes time..", true);
                        progressRing.setCancelable(false);
                        new Thread(new Runnable() {  
                              @Override
                              public void run() {
                                    // TODO Auto-generated method stub
                                    try
                                    {
                                          Thread.sleep(3000);
                                    }catch(Exception e){}
                                    progressRing.dismiss();
                              }
                        }).start();


                //Image choosing logic ================================= 
                imageView.setVisibility(ImageView.VISIBLE);

                if(pTOGGLE)
                    imageView.setImageResource(R.drawable.IMG_XXT);
                else
                    imageView.setImageResource(R.drawable.IMG_XYT);

                wIHM_result.setText(mTS);

            }
        });

问题在于,就好像Image Logic 部分(由标头注释标记)在代码的ProgressDialgo 部分之前运行。显示相应的图像,更改了文本,我可以在 ProgressDialog 使其进度到 N 秒结束时在暗淡的应用程序屏幕中看到所有内容。

我希望它显示更改后的图像和文本(代码中的对象分别为 imageView 和 wIHM),在进度对话框循环完成后,焦点回到应用程序的视图上。

【问题讨论】:

    标签: android progressdialog


    【解决方案1】:

    使用 AsynTask 类。 在其中编写按钮单击并在 AsynTask 中使用进度对话框..

    AsynTask 有类似 initBackground() , preExecute() , postExecute() ..

    在 preExecute 中启动进度对话框...在 initBackground 中编写逻辑,并以 ProgressDialog.dismiss() 结束逻辑最后一行;并在 postExecute() 中编写 textchange 代码和 imagechange 逻辑;

    BUTTON.setOnClickListener(new View.OnClickListener() { 
    
              public void onClick(View v)  {
                   new ABC.Execute();
                }
            });
    public class ABC extends AsyncTask<Void,Void,Void>
    {
    
        ProgressDialog downloadProgressDialog =new ProgressDialog(Accounts.this);
        @Override
        protected void onPostExecute(Void result)
        {
    
            super.onPostExecute(result);
            //Set your text and image object here....
    
        }
    
        @Override
        protected void onPreExecute()
        {
            // TODO Auto-generated method stub
            super.onPreExecute();
            downloadProgressDialog.setMessage("Message");
                downloadProgressDialog.setCancelable(false);
                downloadProgressDialog.show();
        }
    
        @Override
        protected Void doInBackground(Void... arg0) 
        {
    
            //Write your code here......
            downloadProgressDialog.dismiss();
        }
    }
    

    【讨论】:

      【解决方案2】:

      你最好使用 AsyncTask 来做。如果你想根据你的代码解决问题,你应该这样做:

      BUTTON.setOnClickListener(new View.OnClickListener() { 
      
          public void onClick(View v) {
          // TODO Auto-generated method stub
      
          imageView.setVisibility(ImageView.INVISIBLE);
      
          //Choosing MESXs type to show =================================
          if(mTSFlag==1)
              mTS=holA;
          else
              mTS=holB;
      
          // ProgressDialog =================================
          final ProgressDialog progressRing=ProgressDialog.show(MainActivity.this, "Please wait..", "This takes time..", true);
          progressRing.setCancelable(false);
          new Thread(new Runnable() {  
              @Override
              public void run() {
                  // TODO Auto-generated method stub
                  try{
                     Thread.sleep(3000);
      
                     //Don't run your UI method in non-Ui thread.
                     //move progressRing.dismiss() to runOnUiThread();
                     yourActivity.this.runOnUiThread(new Runnable() {
                         @Override
                         public void run() {
                             progressRing.dismiss();
      
                             //Image choosing logic ================================= 
                             imageView.setVisibility(ImageView.VISIBLE);
      
                             if(pTOGGLE)
                                 imageView.setImageResource(R.drawable.IMG_XXT);
                             else
                                 imageView.setImageResource(R.drawable.IMG_XYT);
      
                             wIHM_result.setText(mTS);
                         }
                     }catch(Exception e){
                     }
                 }
             }).start();
         }
      });
      

      【讨论】:

        【解决方案3】:
         progressRing.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                // Image choosing logic codes should be here for processing after dialog showed
            }
         });
        

        setOnShowListener 设置在显示对话框时要调用的侦听器

        【讨论】:

        • 请在代码中添加一些解释它的作用,这也将有助于未来的访问者。
        猜你喜欢
        • 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
        相关资源
        最近更新 更多