【问题标题】:How to show progress dialog in Android?如何在 Android 中显示进度对话框?
【发布时间】:2012-05-13 20:07:25
【问题描述】:

我想在单击登录按钮时显示ProgressDialog,并且需要一些时间才能移动到另一个页面。我该怎么做?

【问题讨论】:

标签: android progressdialog


【解决方案1】:
ProgressDialog pd = new ProgressDialog(yourActivity.this);
pd.setMessage("loading");
pd.show();

这就是你所需要的。

【讨论】:

  • ProgressDialog pd = new ProgressDialog(yourActivity.this); pd.setMessage("加载中"); pd.show();现在显示任何东西。做什么。
  • 您需要使用实际的Activity 而不是yourActivity
【解决方案2】:

你最好试试AsyncTask

示例代码 -

private class YourAsyncTask extends AsyncTask<Void, Void, Void> {
    private ProgressDialog dialog;

    public YourAsyncTask(MyMainActivity activity) {
        dialog = new ProgressDialog(activity);
    }

    @Override
    protected void onPreExecute() {
        dialog.setMessage("Doing something, please wait.");
        dialog.show();
    }
    @Override
    protected Void doInBackground(Void... args) {
        // do background work here
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
         // do UI work here
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}

在您的登录按钮活动中使用上述代码。并且,在doInBackgroundonPostExecute 中做这些事情

更新:

ProgressDialogAsyncTask 集成,正如您所说,您的任务需要时间来处理。

更新:

ProgressDialog 类自 API 26 起已弃用

【讨论】:

  • 他想显示进度对话框,我想不想使用 AsyncTask
  • @AmeerHamza 更新了答案。
  • 无论如何,AysncTask 已被弃用。请更新您的答案。
【解决方案3】:

要使用ProgressDialog,请使用以下代码

ProgressDialog progressdialog = new ProgressDialog(getApplicationContext());
progressdialog.setMessage("Please Wait....");

要启动ProgressDialog 使用

progressdialog.show();

使用progressdialog.setCancelable(false);,以便在工作完成之前无法取消ProgressDialog

要停止ProgressDialog,请使用此代码(当您的工作完成时):

progressdialog.dismiss();`

【讨论】:

    【解决方案4】:

    关于Progress dialog,您应该记住的第一点是您应该在单独的线程中运行它。如果您在 UI 线程中运行它,您将看不到任何对话框。

    如果您不熟悉Android Threading,那么您应该了解AsyncTask。这可以帮助您实现painless Threads

    示例代码

    private class CheckTypesTask extends AsyncTask<Void, Void, Void>{
            ProgressDialog asyncDialog = new ProgressDialog(IncidentFormActivity.this);
            String typeStatus;
    
    
            @Override
            protected void onPreExecute() {
                //set message of the dialog
                asyncDialog.setMessage(getString(R.string.loadingtype));
                //show dialog
                asyncDialog.show();
                super.onPreExecute();
            }
    
            @Override
            protected Void doInBackground(Void... arg0) {
    
                //don't touch dialog here it'll break the application
                //do some lengthy stuff like calling login webservice
    
                return null;
            }
    
            @Override
            protected void onPostExecute(Void result) {
                //hide the dialog
                asyncDialog.dismiss();
    
                super.onPostExecute(result);
            }
    
    }
    

    祝你好运。

    【讨论】:

    • 您对“进度对话框在主 UI 线程上不起作用”的注释让我很开心。谢谢。
    • OnPreExecute 和 onPostExecute 在主线程上运行。您正在从主线程开始和停止进度对话框。
    【解决方案5】:

    activity 中的简单编码如下所示:

    private ProgressDialog dialog = new ProgressDialog(YourActivity.this);    
    dialog.setMessage("please wait...");
    dialog.show();
    dialog.dismiss();
    

    【讨论】:

      【解决方案6】:

      声明你的进度对话框:

      ProgressDialog progressDialog;  
      

      要启动进度对话框:

      progressDialog = ProgressDialog.show(this, "","Please Wait...", true);  
      

      关闭进度对话框:

       progressDialog.dismiss();
      

      【讨论】:

        【解决方案7】:

        ProgressDialog 现在已在 Android O 中正式弃用。我使用来自 https://github.com/Q115/DelayedProgressDialog 的 DelayedProgressDialog 来完成工作。

        用法:

        DelayedProgressDialog progressDialog = new DelayedProgressDialog();
        progressDialog.show(getSupportFragmentManager(), "tag");
        

        【讨论】:

          【解决方案8】:

          这是使用对话框的好方法

          private class YourAsyncTask extends AsyncTask<Void, Void, Void> {
          
             ProgressDialog dialog = new ProgressDialog(IncidentFormActivity.this);
          
             @Override
              protected void onPreExecute() {
                  //set message of the dialog
                  dialog.setMessage("Loading...");
                  //show dialog
                  dialog.show();
                  super.onPreExecute();
              }
          
             protected Void doInBackground(Void... args) {
              // do background work here
              return null;
             }
          
             protected void onPostExecute(Void result) {
               // do UI work here
               if(dialog != null && dialog.isShowing()){
                 dialog.dismiss()
               }
          
            }
          }
          

          【讨论】:

            【解决方案9】:

            当你调用 oncreate() 时

            new LoginAsyncTask ().execute();
            

            这里如何在流中使用..

            ProgressDialog progressDialog;
            
              private class LoginAsyncTask extends AsyncTask<Void, Void, Void> {
              @Override
                protected void onPreExecute() {
                    progressDialog= new ProgressDialog(MainActivity.this);
                    progressDialog.setMessage("Please wait...");
                    progressDialog.show();
                    super.onPreExecute();
                }
            
                 protected Void doInBackground(Void... args) {
                    // Parsse response data
                    return null;
                 }
            
                 protected void onPostExecute(Void result) {
                    if (progressDialog.isShowing())
                                    progressDialog.dismiss();
                    //move activity
                    super.onPostExecute(result);
                 }
             }
            

            【讨论】:

              【解决方案10】:
              ProgressDialog dialog = 
                 ProgressDialog.show(yourActivity.this, "", "Please Wait...");
              

              【讨论】:

                【解决方案11】:
                ProgressDialog pd = new ProgressDialog(yourActivity.this);
                pd.show();
                

                【讨论】:

                • 虽然这确实提供了对问题的直接答案,但这通常会在 Stack Overflow 上被视为低质量,因为它没有提供上下文或解释。请考虑扩展您的答案。
                【解决方案12】:

                第 1 步:创建 XML 文件

                <?xml version="1.0" encoding="utf-8"?>
                <LinearLayout
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
                
                
                    <Button
                        android:id="@+id/btnProgress"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="Progress Dialog"/>
                </LinearLayout>
                

                第 2 步:创建 SampleActivity.java

                package com.scancode.acutesoft.telephonymanagerapp;
                
                
                import android.app.Activity;
                import android.app.ProgressDialog;
                import android.os.Bundle;
                import android.view.View;
                import android.widget.Button;
                
                public class SampleActivity extends Activity implements View.OnClickListener {
                    Button btnProgress;
                    @Override
                    protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);
                
                        btnProgress = (Button) findViewById(R.id.btnProgress);
                        btnProgress.setOnClickListener(this);
                    }
                
                    @Override
                    public void onClick(View v) {
                
                        final ProgressDialog progressDialog = new ProgressDialog(SampleActivity.this);
                        progressDialog.setMessage("Please wait data is Processing");
                        progressDialog.show();
                
                //        After 2 Seconds i dismiss progress Dialog
                
                        new Thread(){
                            @Override
                            public void run() {
                                super.run();
                                try {
                                    Thread.sleep(2000);
                                    if (progressDialog.isShowing())
                                        progressDialog.dismiss();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }.start();
                    }
                }
                

                【讨论】:

                  【解决方案13】:
                    final ProgressDialog loadingDialog = ProgressDialog.show(context,
                       "Fetching BloodBank List","Please wait...",false,false);  // for  showing the 
                      // dialog  where context is the current context, next field is title followed by
                      // message to be shown to the user and in the end intermediate field
                      loadingDialog.dismiss();// for dismissing the dialog 
                  

                  了解更多信息 Android - What is difference between progressDialog.show() and ProgressDialog.show()?

                  【讨论】:

                    【解决方案14】:

                    ProgressDialog 自 API 26 起已弃用

                    你仍然可以使用这个:

                    public void button_click(View view)
                    {
                        final ProgressDialog progressDialog = ProgressDialog.show(Login.this,"Please Wait","Processing...",true);
                    }
                    

                    【讨论】:

                      【解决方案15】:

                      简单的方法:

                      ProgressDialog pDialog = new ProgressDialog(MainActivity.this); //Your Activity.this
                      pDialog.setMessage("Loading...!");
                      pDialog.setCancelable(false);
                      pDialog.show();
                      

                      【讨论】:

                        【解决方案16】:
                                final ProgressDialog progDailog = ProgressDialog.show(Inishlog.this, contentTitle, "even geduld aub....", true);//please wait....
                        
                                final Handler handler = new Handler() {
                                    @Override
                                    public void handleMessage(Message msg) {
                                        Barcode_edit.setText("");
                                        showAlert("Product detail saved.");
                        
                        
                                    }
                        
                                };
                        
                                new Thread() {
                                    public void run() {
                                        try {
                                 } catch (Exception e) {
                        
                                        }
                                        handler.sendEmptyMessage(0);
                                        progDailog.dismiss();
                                    }
                                }.start();
                        

                        【讨论】:

                          【解决方案17】:

                          只要你想ProgressDialog调用这个方法

                              private void startLoader() {
                              progress = new ProgressDialog(this);    //ProgressDialog
                              progress.setTitle("Loading");
                              progress.setMessage("Please wait");
                              progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                              progress.setCancelable(false);
                              progress.show();
                              new Thread(new Runnable() {
                                  public void run() {
                                      try {
                                          Thread.sleep(7000);
                                      } catch (Exception e) {
                                          e.printStackTrace();
                                      }
                                      progress.dismiss();
                                  }
                              }).start();
                          }
                          

                          【讨论】:

                            猜你喜欢
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 2016-05-21
                            • 1970-01-01
                            • 1970-01-01
                            • 2012-06-11
                            • 1970-01-01
                            • 1970-01-01
                            相关资源
                            最近更新 更多