我想ProgressDialog
在单击登录按钮时显示,移动到另一个页面需要时间。我怎样才能做到这一点?
17 回答
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 线程新手,那么您应该了解AsyncTask。这可以帮助您实现无痛的 Threads。
示例代码
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();
ProgressDialog 现在已在 Android O 中正式弃用。我使用https://github.com/Q115/DelayedProgressDialog中的 DelayedProgressDialog 来完成工作。
用法:
DelayedProgressDialog progressDialog = new DelayedProgressDialog();
progressDialog.show(getSupportFragmentManager(), "tag");
这是使用对话框的好方法
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()
}
}
}
当你调用 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);
}
}
ProgressDialog dialog =
ProgressDialog.show(yourActivity.this, "", "Please Wait...");
ProgressDialog pd = new ProgressDialog(yourActivity.this);
pd.show();
步骤 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>
第二步:创建一个 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();
}
}
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() 有什么区别?
自 API 26 起已弃用 ProgressDialog
你仍然可以使用这个:
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();
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();
每当你想要ProgressDialog调用这个方法
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();
}