我想在单击“登录”按钮时显示
ProgressDialog
,并且需要一些时间才能移动到另一个页面。我该怎么做?
ProgressDialog pd = new ProgressDialog(yourActivity.this);
pd.setMessage("loading");
pd.show();
这就是您所需要的。
你最好尝试使用 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();
}
}
}
在您的登录按钮活动中使用上述代码。并且,做
doInBackground
和onPostExecute
中的事情
更新:
ProgressDialog
与 AsyncTask
集成,正如您所说,您的任务需要时间来处理。
更新:
ProgressDialog
类自 API 26 起已弃用
要使用
ProgressDialog
,请使用以下代码
ProgressDialog progressdialog = new ProgressDialog(getApplicationContext());
progressdialog.setMessage("Please Wait....");
要启动
ProgressDialog
使用
progressdialog.show();
使用 progressdialog.setCancelable(false);
,以便在工作完成之前无法取消 ProgressDialog
。
要停止
ProgressDialog
,请使用此代码(当您的工作完成时):
progressdialog.dismiss();`
当谈到进度对话框时,您应该记住的第一点是您应该在单独的线程中运行它。如果您在 UI 线程中运行它,您将看不到任何对话框。
如果您是Android Threading的新手,那么您应该了解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);
}
}
祝你好运。
activity
中进行简单编码,如下所示:
private ProgressDialog dialog = new ProgressDialog(YourActivity.this);
dialog.setMessage("please wait...");
dialog.show();
dialog.dismiss();
ProgressDialog progressDialog;
启动进度对话框:
progressDialog = ProgressDialog.show(this, "","Please Wait...", true);
关闭进度对话框:
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()
}
}
}
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);
}
}
<?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>
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();
}
}
你仍然可以使用这个:
public void button_click(View view)
{
final ProgressDialog progressDialog = ProgressDialog.show(Login.this,"Please Wait","Processing...",true);
}
ProgressDialog pDialog = new ProgressDialog(MainActivity.this); //Your Activity.this
pDialog.setMessage("Loading...!");
pDialog.setCancelable(false);
pDialog.show();
就调用这个方法
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();
}
注意:与Android 33一起使用。
private AlertDialog progressAlertDialog = createProgressDialog(currentActivity / this);
private AlertDialog createProgressDialog(AppCompatActivity currentActivity) {
LinearLayout vLayout = new LinearLayout(currentActivity);
vLayout.setOrientation(LinearLayout.VERTICAL);
vLayout.setPadding(50, 50, 50, 50);
vLayout.addView(new ProgressBar(currentActivity, null, android.R.attr.progressBarStyleLarge));
return new AlertDialog.Builder(currentActivity)
.setCancelable(false)
.setView(vLayout)
.create();
}
public void displayProgressDialog() {
if (!progressAlertDialog.isShowing()) {
progressAlertDialog.show();
}
}
public void hideProgressDialog() {
progressAlertDialog.dismiss();
}