【问题标题】:Cancel or dismiss android alert dialogue取消或关闭 android 警报对话框
【发布时间】:2013-05-17 11:18:18
【问题描述】:

我正在使用以下函数在我的 android 视图中创建/显示加载

public void showDialogue(String Message, String Title){
    builder = new AlertDialog.Builder(this);
    progress = new ProgressBar(this);
    builder.setMessage(Message);
    builder.setView(progress);
    builder.create().show();

}

我将此函数称为异步任务

private class SetParm extends AsyncTask<String, Integer, String> {

        Integer myid;
        Integer myFlag;
        Integer removeId;
        @Override
        protected String doInBackground(String... sUrl) {
            try {

                SharedPreferences mPrefs = getSharedPreferences("prefs",0);    
                String restoredText = mPrefs.getString("access_token", ""); 
                String path = "http://www.sitename.com/app/setFlag.php";

                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout

                HttpResponse response;
                JSONObject json = new JSONObject();
                try {
                    HttpPost post = new HttpPost(path);
                    json.put("access_token", restoredText);
                    json.put("id", myid);
                    json.put("flag", myFlag);

                    Log.i("jason Object", json.toString());
                    post.setHeader("json", json.toString());
                    StringEntity se = new StringEntity(json.toString());
                    se.setContentEncoding((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                    post.setEntity(se);
                    response = client.execute(post);
                    /* Checking response */
                    if (response != null) {
                        InputStream in = response.getEntity().getContent(); 

                        String a = convertStreamToString(in);

                        JSONObject jsono = stringToJsonobj(a);
                        Log.v("TAGG",a);
                        String passedStringValue = jsono.getString("result");


                        if(passedStringValue.equals("1")){
                            flags=1;
                            //Log.v("TAGG", "Success");
                            SharedPreferences mPrefss = getSharedPreferences("prefs", 0);    
                            SharedPreferences.Editor editor = mPrefss.edit();    
                            editor.putString("access_token", jsono.getString("access_token"));  
                            editor.commit();
                        }
                        else {
                            flags=0;
                            //Log.v("TAGG", "Failed !");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


            } catch (Exception e) {
            }
            return null;
        }


        @Override
        protected void onPreExecute() {
            showDialogue("Regestring Your Devide... Please wait.", "Regestring Devide");

            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
        }



        @Override
        protected void onPostExecute(String result) {
            builder.cancel();
            if(flags.equals(1)){
                TableLayout mTable = (TableLayout)findViewById(R.id.tableLayout_1);
                mTable.removeView(findViewById(4500+removeId));
                mTable.removeView(findViewById(6500+removeId));

                int count = mTable.getChildCount();
                if(count<=0){
                    lists="";
                    total="0";
                    LogIN loginUsers1 = new LogIN();
                    loginUsers1.execute("");

                }

            }
            else {
                TextView text = (TextView) findViewById(R.id.status_msg);
                text.setText("Error while processing requests. Please try again.");
            }
            super.onPostExecute(result);
        }

    }

从 onPreExecute() 函数调用警报对话框。

现在我需要在 web 服务请求完成后删除加载

所以我在onPostExecute 中写了builder.cancel();,但它不起作用

有什么想法吗? 提前致谢

【问题讨论】:

    标签: android web-services android-asynctask


    【解决方案1】:

    可以用progress dialog代替普通的dialog,如下图:

    private ProgressDialog progressDialog;
    
    protected void onPreExecute() {
                super.onPreExecute();
                progressDialog= new ProgressDialog(MainActivity.this);
                progressDialog.setMessage("Loading ....");
                progressDialog.setIndeterminate(false);
                progressDialog.setCancelable(true);
                progressDialog.show();
            }
    
     protected void onPostExecute(String result) {
    progressDialog.dismiss();
    }
    

    【讨论】:

      【解决方案2】:

      进度条的自定义.....

               //Pre Execute method
      
                  @Override
                  protected void onPreExecute()
                  {
                      super.onPreExecute();
                      ProgressDialog pDialog = new ProgressDialog(Activity.this);
      
                      pDialog= ProgressDialog.show(.this, "","Loading.    Please wait...", true);
                      pDialog.setContentView(R.layout.progressbar);
      
                      pDialog.setCancelable(false);
      
                  }
      
          ////////////////////////////////////////////////////////////////////////////////////////
      
              Progress bar xml layout
              that is progressbar.xml
      
          <?xml version="1.0" encoding="utf-8"?>
          <RelativeLayout 
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="150dip"
              android:layout_height="150dip"
              android:background="@drawable/progressbar_shap"
              android:layout_gravity="center">
      
            <TextView  android:id="@+id/textview"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignParentBottom="true"
             android:text="Please Wait..."
             android:textStyle="bold"
            />
            <ProgressBar 
             android:indeterminateDrawable="@drawable/my_progress_indetermine"
             android:layout_height="60dp" 
             android:layout_width="60dp"
             android:layout_centerInParent="true"
             ></ProgressBar>
          </RelativeLayout>
      
      
        ////////////////////////////////////////////////////////////////////////
        After that 
        my_progressbar_indertimine.xml
      
          <?xml version="1.0" encoding="utf-8"?>
          <animated-rotate 
           xmlns:android="http://schemas.android.com/apk/res/android"
           android:drawable="@drawable/please_wait"
           android:pivotX="50%"
           android:pivotY="50%" />
      
      
      
      
      
      //Post execute method , which will dismiss progress bar.
      
      
      @Override
              protected void onPostExecute(String file_url) {
                  // dismiss the dialog after getting all albums
                  pDialog.dismiss();
      }
      

      “please_wait”进度条图片,它会显示白色,当你将使用下面的链接 ,但你可以保存它,在浏览器页面上右击并使用“另存为”

        http://i.stack.imgur.com/hnY8r.png
      

      【讨论】:

        【解决方案3】:

        这样试试

            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(Activity.this);
                pDialog.setMessage("Loading Please wait...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }   
        

        在 onPostExecute 中这样做

             protected void onPostExecute(String result) {
                super.onPostExecute(toString());
                // dismiss the dialog after loading
                pDialog.dismiss();          
                    }
        

        【讨论】:

        • 感谢您的回复.. 我已经尝试过了.. 但是在关闭时出错
        • AlertDialog.Builder 没有 dismiss() 方法。
        • @OneManArmy 你也可以通过 pDialog.setMax(100); pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        【解决方案4】:

        您可以使用Dialog 代替AlertDialog 并在onPostExecute() 中调用dialog.dismiss()

        编辑:请不要将此答案视为解决方案。因为我认为 ramesh 使用的是AlertDialog.Builder,并且回答错误。与Dialog 一样,我们有更多自定义选项,我建议这样做。

        【讨论】:

        • 感谢您的回复...你能给我一个示例代码吗?我是安卓新手
        【解决方案5】:

        在你的 onPostExecute 中试试这个.....

            @Override
            protected void onPostExecute(String result) {
                try{
                builder.dismiss();
                 }
              catch(Exception e){
        
                    }
                if(flags.equals(1)){
                    TableLayout mTable = (TableLayout)findViewById(R.id.tableLayout_1);
                    mTable.removeView(findViewById(4500+removeId));
                    mTable.removeView(findViewById(6500+removeId));
        
                    int count = mTable.getChildCount();
                    if(count<=0){
                        lists="";
                        total="0";
                        LogIN loginUsers1 = new LogIN();
                        loginUsers1.execute("");
        
                    }
        
                }
                else {
                    TextView text = (TextView) findViewById(R.id.status_msg);
                    text.setText("Error while processing requests. Please try again.");
                }
        
            }
        

        【讨论】:

          【解决方案6】:

          试试这个代码的异步任务和进度对话框,

              public class MyTask extends AsyncTask<Void, Integer, Void> {
          
              public MyTask(Activity activity, int id) {
                  this.activity = activity;
          
          
                  context = activity;
          
              }
          
              /** progress dialog to show user that the backup is processing. */
              private ProgressDialog progressDialog;
              /** application context. */
              private Activity activity;
              private Context context;
          
          
              protected void onPreExecute() {
                  // TODO Auto-generated method stub
                  super.onPreExecute();
          
                  progressDialog = ProgressDialog.show(context, "", "Loading...");
          
          
          
              }
          
              @Override
              protected Void doInBackground(Void... params) {
                  // TODO Auto-generated method stub
          
                              return null;
              }
          
              @Override
              protected void onPostExecute(Void result) {
                  // TODO Auto-generated method stub
                  super.onPostExecute(result);
                              progressDialog.dismiss();
          
                       }
          
          }
          

          【讨论】:

            猜你喜欢
            • 2015-04-08
            • 1970-01-01
            • 1970-01-01
            • 2013-05-11
            • 2018-07-22
            • 1970-01-01
            • 2015-07-11
            • 2018-04-11
            • 1970-01-01
            相关资源
            最近更新 更多