如何在Android中显示进度对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10446125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show progress dialog in Android?
提问by Jave
I want to show ProgressDialog
when I click on Login button and it takes time to move to another page. How can I do this?
我想ProgressDialog
在我点击登录按钮时显示,移动到另一个页面需要时间。我怎样才能做到这一点?
采纳答案by Praveenkumar
You better try with AsyncTask
你最好试试AsyncTask
Sample code -
示例代码 -
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();
}
}
}
Use the above code in your Login Button Activity. And, do the stuff in doInBackground
and onPostExecute
在您的登录按钮活动中使用上述代码。并且,在doInBackground
和做这些事情onPostExecute
Update:
更新:
ProgressDialog
is integrated with AsyncTask
as you said your task takes time for processing.
ProgressDialog
集成了AsyncTask
如你所说你的任务需要时间进行处理。
Update:
更新:
ProgressDialog
class was deprecated as of API 26
ProgressDialog
自 API 26 起不推荐使用类
回答by Jave
ProgressDialog pd = new ProgressDialog(yourActivity.this);
pd.setMessage("loading");
pd.show();
And that's all you need.
这就是你所需要的。
回答by Abhishek Punia
To use ProgressDialog
use the below code
要使用ProgressDialog
使用下面的代码
ProgressDialog progressdialog = new ProgressDialog(getApplicationContext());
progressdialog.setMessage("Please Wait....");
To start the ProgressDialog
use
开始ProgressDialog
使用
progressdialog.show();
progressdialog.setCancelable(false);
is used so that ProgressDialog
cannot be cancelled until the work is done.
progressdialog.setCancelable(false);
使用,以便ProgressDialog
在工作完成之前无法取消。
To stop the ProgressDialog
use this code (when your work is finished):
要停止ProgressDialog
使用此代码(当您的工作完成时):
progressdialog.dismiss();`
回答by Jay Mayu
Point one you should remember when it comes to Progress dialogis that you should run it in a separate thread. If you run it in your UI thread you'll see no dialog.
当谈到“进度”对话框时,您应该记住的第一点是您应该在单独的线程中运行它。如果您在 UI 线程中运行它,您将看不到对话框。
If you are new to Android Threadingthen you should learn about AsyncTask. Which helps you to implement a painless Threads.
如果您不熟悉Android 线程,那么您应该了解AsyncTask。这可以帮助您实现无痛 Threads。
sample code
示例代码
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);
}
}
Good luck.
祝你好运。
回答by Chirag Patel
Simple coding in your activity
like below:
activity
像下面这样的简单编码:
private ProgressDialog dialog = new ProgressDialog(YourActivity.this);
dialog.setMessage("please wait...");
dialog.show();
dialog.dismiss();
回答by Md. Ilyas Hasan Mamun
Declare your progress dialog:
声明您的进度对话框:
ProgressDialog progressDialog;
To start the progress dialog:
要启动进度对话框:
progressDialog = ProgressDialog.show(this, "","Please Wait...", true);
To dismiss the Progress Dialog :
要关闭进度对话框:
progressDialog.dismiss();
回答by Aristo Michael
This is the good way to use dialog
这是使用对话框的好方法
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()
}
}
}
回答by Kush
when you call in oncreate()
当你调用 oncreate()
new LoginAsyncTask ().execute();
Here how to use in flow..
这里如何在流中使用..
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);
}
}
回答by NinjaCowgirl
ProgressDialog is now officially deprecated in Android O. I use DelayedProgressDialog from https://github.com/Q115/DelayedProgressDialogto get the job done.
ProgressDialog 现在在 Android O 中正式弃用。我使用来自https://github.com/Q115/DelayedProgressDialog 的DelayedProgressDialog来完成工作。
Usage:
用法:
DelayedProgressDialog progressDialog = new DelayedProgressDialog();
progressDialog.show(getSupportFragmentManager(), "tag");
回答by Anish Arya
ProgressDialog is deprecated since API 26
自 API 26 起不推荐使用 ProgressDialog
still you can use this:
你仍然可以使用这个:
public void button_click(View view)
{
final ProgressDialog progressDialog = ProgressDialog.show(Login.this,"Please Wait","Processing...",true);
}