Android - Create Progress Dialog(Android - 创建进度对话框)
问题描述
我是安卓开发新手.我想在我的应用程序中开发一个带有 progressbar
的 dialog
.当我单击搜索按钮时,dialog
应该与 progressbar
一起出现,显示在切换到另一个 activity
之前进度正在进行.请用示例代码向我推荐.
I am new to android development. I want to develop a dialog
with a progressbar
in my application. When i click the search button the dialog
should appear with the progressbar
, showing that the progress is going on before switching to another activity
. Please suggest me with sample code.
推荐答案
使用 ProgressDialog
.不过,您应该在一个新的 thread
上完成这项工作,并在完成后使用 handler
回调 activity
.这是我的做法:
Use a ProgressDialog
. You should do the work on a new thread
, though, and use a handler
to call back to the activity
when finished. Here's how I do it:
private ProgressDialog pd;
private View.OnClickListener searchClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
pd = ProgressDialog.show(MyActivity.this, "Searching...", "Searching for matches", true, false);
new Thread(new Runnable() {
public void run() {
//do work
//.....
finishedHandler.sendEmptyMessage();
}
}).start();
}
}
private Handler finishedHandler = new Handler() {
@Override public void handleMessage(Message msg) {
pd.dismiss();
//start new activity
}
}
这篇关于Android - 创建进度对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android - 创建进度对话框


基础教程推荐
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01