如何显示在Android的进度对话框?如何显示在Android的进度对话框?(How to show

2019-05-13 20:18发布

我想告诉ProgressDialog当我点击登录按钮,它需要时间来移动到另一页。 我怎样才能做到这一点?

Answer 1:

你更好地与尝试的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



Answer 2:

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

这就是你所需要的。



Answer 3:

当谈到你应该记住一点一个进度对话框是,你应该在一个单独的线程中运行它。 如果您在UI线程中运行它,你会看到没有对话。

如果你是新来的Android线程 ,那么你应该了解的AsyncTask 。 它可以帮助您实现无痛线程 。

示例代码

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

}

祝好运。



Answer 4:

要使用ProgressDialog使用下面的代码

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

要启动ProgressDialog使用

progressdialog.show();

progressdialog.setCancelable(false); 用来使ProgressDialog不能被取消,直到工作完成。

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

progressdialog.dismiss();`


Answer 5:

这是使用对话的好方法

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

  }
}


Answer 6:

当你的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);
     }
 }


Answer 7:

简单的编码在您的activity象下面这样:

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


Answer 8:

声明你的进度对话框:

ProgressDialog progressDialog;  

要启动进度对话框:

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

要关闭进度对话框:

 progressDialog.dismiss();


Answer 9:

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


Answer 10:

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


Answer 11:

  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()之间的区别?



Answer 12:

ProgressDialog现正式的Android O.弃用我用DelayedProgressDialog从https://github.com/Q115/DelayedProgressDialog来完成这项工作。

用法:

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


Answer 13:

因为API 26 ProgressDialog已被弃用

你仍然可以使用这个:

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


Answer 14:

简单的方法:

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


Answer 15:

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


Answer 16:

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


文章来源: How to show progress dialog in Android?