我想ProgressDialog在单击"登录"按钮时显示,并且需要时间才能移动到另一个页面.我怎样才能做到这一点?
Jav*_*ave 97
ProgressDialog pd = new ProgressDialog(yourActivity.this);
pd.setMessage("loading");
pd.show();
Run Code Online (Sandbox Code Playgroud)
这就是你所需要的.
Pra*_*mar 48
您最好尝试使用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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在"登录按钮活动"中使用上述代码.而且,这样做的东西在doInBackground和onPostExecute
更新:
ProgressDialog集成了AsyncTask如你所说你的任务需要时间进行处理.
更新:
ProgressDialog 自API 26起,class已被弃用
小智 22
要使用ProgressDialog以下代码
ProgressDialog progressdialog = new ProgressDialog(getApplicationContext());
progressdialog.setMessage("Please Wait....");
Run Code Online (Sandbox Code Playgroud)
开始ProgressDialog使用
progressdialog.show();
Run Code Online (Sandbox Code Playgroud)
progressdialog.setCancelable(false);使用,以便ProgressDialog在工作完成之前不能取消.
要停止ProgressDialog使用此代码(当您的工作完成时):
progressdialog.dismiss();`
Run Code Online (Sandbox Code Playgroud)
Jay*_*ayu 19
在进度对话框中你应该记住的第一点是你应该在一个单独的线程中运行它.如果您在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);
}
}
Run Code Online (Sandbox Code Playgroud)
祝好运.
activity像下面这样的简单编码:
private ProgressDialog dialog = new ProgressDialog(YourActivity.this);
dialog.setMessage("please wait...");
dialog.show();
dialog.dismiss();
Run Code Online (Sandbox Code Playgroud)
声明您的进度对话框:
ProgressDialog progressDialog;
Run Code Online (Sandbox Code Playgroud)
要启动进度对话框:
progressDialog = ProgressDialog.show(this, "","Please Wait...", true);
Run Code Online (Sandbox Code Playgroud)
要关闭进度对话框:
progressDialog.dismiss();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
180160 次 |
| 最近记录: |