62

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

4

17 回答 17

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

这就是你所需要的。

于 2012-05-04T09:25:40.873 回答
67

您最好尝试使用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你所说,你的任务需要时间来处理。

更新:

ProgressDialog自 API 26 起,该类已被弃用

于 2012-05-04T09:31:01.303 回答
26

要使用ProgressDialog以下代码

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

开始ProgressDialog使用

progressdialog.show();

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

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

progressdialog.dismiss();`
于 2016-06-24T10:46:36.577 回答
19

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

如果您是Android 线程新手,那么您应该了解AsyncTask。这可以帮助您实现无痛的 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);
        }

}

祝你好运。

于 2012-05-04T09:35:47.040 回答
7

如下所示的简单编码activity

private ProgressDialog dialog = new ProgressDialog(YourActivity.this);    
dialog.setMessage("please wait...");
dialog.show();
dialog.dismiss();
于 2018-10-03T09:44:16.810 回答
5

声明你的进度对话框:

ProgressDialog progressDialog;  

要启动进度对话框:

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

要关闭进度对话框:

 progressDialog.dismiss();
于 2014-07-22T05:36:23.360 回答
3

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

用法:

DelayedProgressDialog progressDialog = new DelayedProgressDialog();
progressDialog.show(getSupportFragmentManager(), "tag");
于 2017-09-17T08:20:41.530 回答
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()
     }

  }
}
于 2014-04-05T09:44:14.140 回答
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);
     }
 }
于 2016-06-24T10:57:48.190 回答
1
ProgressDialog dialog = 
   ProgressDialog.show(yourActivity.this, "", "Please Wait...");
于 2012-05-04T09:31:30.150 回答
1
ProgressDialog pd = new ProgressDialog(yourActivity.this);
pd.show();
于 2015-04-18T07:46:43.307 回答
1

步骤 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>

第二步:创建一个 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();
    }
}
于 2016-04-18T11:17:30.860 回答
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 - progressDialog.show() 和 ProgressDialog.show() 有什么区别?

于 2017-03-11T08:20:19.207 回答
1

自 API 26 起已弃用 ProgressDialog

你仍然可以使用这个:

public void button_click(View view)
{
    final ProgressDialog progressDialog = ProgressDialog.show(Login.this,"Please Wait","Processing...",true);
}
于 2018-01-11T08:07:55.113 回答
1

简单的方法:

ProgressDialog pDialog = new ProgressDialog(MainActivity.this); //Your Activity.this
pDialog.setMessage("Loading...!");
pDialog.setCancelable(false);
pDialog.show();
于 2018-05-08T05:26:14.397 回答
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();
于 2012-05-04T09:42:04.273 回答
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();
}
于 2021-07-28T09:25:03.023 回答