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

我想显示进度对话框,当我点击loginbutton,它需要时间移动到另一个页面。请为它发布一些示例代码。

Solutions Collecting From Web of "如何在Android中显示进度对话框?"

你最好用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(); } protected Void doInBackground(Void... args) { // do background work here return null; } protected void onPostExecute(Void result) { // do UI work here if (dialog.isShowing()) { dialog.dismiss(); } } } 

在您的loginbutton活动中使用上面的代码。 而且,做doInBackgroundonPostExecute的东西

更新:

ProgressDialogAsyncTask集成在一起,就像你说的任务需要时间来处理。

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

这就是你所需要的。

点击“ 进度”对话框 ,您应该记住,您应该在单独的线程中运行它。 如果你在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); } } 

祝你好运。

要使用ProgressDialog使用下面的代码

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

开始使用ProgressDialog

 progressdialog.show(); 

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

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

 progressdialog.dismiss();` 

这是使用对话框的好方法

 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() } } } 
 ProgressDialog dialog = ProgressDialog.show(yourActivity.this, "", "Please Wait..."); 

声明你的进度对话框:

  ProgressDialog progressDialog; 

要启动进度对话框:

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

要closures进度对话框:

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

当你在oncreate()中调用时

 new LoginAsyncTask ().execute(); 

这里如何使用stream量..

 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); } } 
  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()之间有什么区别?

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

第1步:创build一个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步:创build一个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(); } } 

ProgressDialog现在在Android O中正式被弃用。我使用https://github.com/Q115/DelayedProgressDialog中&#x7684; DelayedProgressDialog来完成工作。

用法:

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