如何在Android中显示进度对话框?

时间:2012-05-04 09:22:42

标签: android progressdialog

我想在点击“登录”按钮时显示ProgressDialog,移动到另一个页面需要时间。我怎么能这样做?

17 个答案:

答案 0 :(得分:89)

ProgressDialog pd = new ProgressDialog(yourActivity.this);
pd.setMessage("loading");
pd.show();

这就是你所需要的。

答案 1 :(得分:41)

您最好尝试使用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

中的内容

<强>更新

ProgressDialog已与AsyncTask集成,因为您说您的任务需要时间进行处理。

<强>更新

自API 26起,

ProgressDialog类已弃用

答案 2 :(得分:19)

关于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);
        }

}
祝你好运。

答案 3 :(得分:18)

要使用ProgressDialog progressdialog = new ProgressDialog(getApplicationContext()); progressdialog.setMessage("Please Wait...."); ,请使用以下代码

ProgressDialog

开始progressdialog.show(); 使用

progressdialog.setCancelable(false);
使用了

ProgressDialog,以便在完成工作之前无法取消ProgressDialog

要停止progressdialog.dismiss();` 使用此代码(当您的工作完成时):

EditText.getText()

答案 4 :(得分:3)

声明您的进度对话框:

ProgressDialog progressDialog;  

开始进度对话框:

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

取消进度对话框:

 progressDialog.dismiss();

答案 5 :(得分:3)

activity中进行简单编码,如下所示:

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

答案 6 :(得分:2)

这是使用对话框的好方法

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()
     }

  }
}

答案 7 :(得分:2)

当你打电话给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);
     }
 }

答案 8 :(得分:1)

ProgressDialog dialog = 
   ProgressDialog.show(yourActivity.this, "", "Please Wait...");

答案 9 :(得分:1)

ProgressDialog pd = new ProgressDialog(yourActivity.this);
pd.show();

答案 10 :(得分:1)

  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()?

答案 11 :(得分:1)

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

用法:

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

答案 12 :(得分:1)

自API 26以来,不推荐使用ProgressDialog

你仍然可以使用它:

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

答案 13 :(得分:1)

简单方法:

sslCAinfo = /bin/curl-ca-bundle.crt

答案 14 :(得分:0)

只要你想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();
}

答案 15 :(得分:0)

第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();
    }
}

答案 16 :(得分:0)

        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();