【发布时间】:2013-08-02 12:05:25
【问题描述】:
我有一个功能,可以在我的手机上更新联系人列表。当联系人更新时,我需要放置一个进度条。我需要在更新功能中添加一个进度条。我怎么做?更新功能运行良好,我只需要一个进度条来显示它正在更新。请帮忙。
public void onClick(View v){
//reset progress bar status
progressBarStatus = 0;
//reset filesize
fileSize = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
// process some tasks
progressBarStatus = UpdateContact();
// your computer is too fast, sleep 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
// ok, file is downloaded,
if (progressBarStatus >= 100) {
// sleep 2 seconds, so that you can see the 100%
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// close the progress bar dialog
progressBar.dismiss();
}
}
}).start();
}
});
【问题讨论】:
-
你为什么不放一个旋转的圆圈。这将更直观,如果您放置进度条,用户将难以理解正在发生的事情:)
-
这里有一个很好的例子-> stackoverflow.com/questions/15585749/… .
-
或者简单地 -
<ProgressBar android:id="@+id/pb" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_centerInParent="true" android:layout_height="wrap_content" />并使用它的可见性(例如,在onCreate()中将其设置为 GONE,然后在onClick()的开头将其设置为 VISIBLE(以防万一) ) 并在最后再次将其设置为 GONE)。
标签: android progress-bar