- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试在进度条加载到特定百分比时添加消息。因此,当加载了 10% 时,会出现一个标题或一条消息,表明某些内容已加载。我做不到,它强制关闭。任何想法如何去做。
下面是我的示例代码
public void onClick(View v) {
// prepare for a progress bar dialog
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("File downloading ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
//getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
//reset progress bar status
progressBarStatus = 0;
//reset filesize
fileSize = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
// process some tasks
progressBarStatus = doSomeTasks();
// 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();
}
});
}
// file download simulator... a really simple
public int doSomeTasks() {
while (fileSize <= 1000000) {
fileSize++;
setProgressBarIndeterminate(true);
if (fileSize == 100000) {
progressBar.setMessage("10 percent loaded");
return 10;
} else if (fileSize == 200000) {
progressBar.setMessage("20 percent loaded");
return 20;
} else if (fileSize == 300000) {
progressBar.setMessage("30 percent loaded");
return 30;
}
// ...add your own
}
return 100;
}
感谢 StackOverFlow 用户
最佳答案
尝试像下面的代码一样更新进度条...
//To use the AsyncTask, it must be subclassed
private class LoadViewTask extends AsyncTask<Void, Integer, Void>
{
//Before running code in separate thread
@Override
protected void onPreExecute()
{
//Create a new progress dialog
progressDialog = new ProgressDialog(LoadingScreenActivity.this);
//Set the progress dialog to display a horizontal progress bar
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//Set the dialog title to 'Loading...'
progressDialog.setTitle("Loading...");
//Set the dialog message to 'Loading application View, please wait...'
progressDialog.setMessage("Loading application View, please wait...");
//This dialog can't be canceled by pressing the back key
progressDialog.setCancelable(false);
//This dialog isn't indeterminate
progressDialog.setIndeterminate(false);
//The maximum number of items is 100
progressDialog.setMax(100);
//Set the current progress to zero
progressDialog.setProgress(0);
//Display the progress dialog
progressDialog.show();
}
//The code to be executed in a background thread.
@Override
protected Void doInBackground(Void... params)
{
/* This is just a code that delays the thread execution 4 times,
* during 850 milliseconds and updates the current progress. This
* is where the code that is going to be executed on a background
* thread must be placed.
*/
try
{
//Get the current thread's token
synchronized (this)
{
//Initialize an integer (that will act as a counter) to zero
int counter = 0;
//While the counter is smaller than four
while(counter <= 4)
{
//Wait 850 milliseconds
this.wait(850);
//Increment the counter
counter++;
//Set the current progress.
//This value is going to be passed to the onProgressUpdate() method.
publishProgress(counter*25);
}
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return null;
}
//Update the progress
@Override
protected void onProgressUpdate(Integer... values)
{
//set the current progress of the progress dialog
progressDialog.setProgress(values[0]);
}
//after executing the code in the thread
@Override
protected void onPostExecute(Void result)
{
//close the progress dialog
progressDialog.dismiss();
//initialize the View
setContentView(R.layout.main);
}
}
并在您想要显示进度条的位置调用此 AsyncTask...
//Initialize a LoadViewTask object and call the execute() method
new LoadViewTask().execute();
关于java - 加载百分比时 Android ProgressBar 消息发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12490965/
我的应用程序中有一个 ProgressBar 小部件,无论我做什么,它总是显示为粗橙色(2.x 样式)而不是细蓝色(4.x 样式) . 项目构建目标设置为 Google API 4.0.3(API 级
private static ProgressDialog pDialog; pDialog = new ProgressDialog(activity); pDialog.setIndetermin
我试图让 TalkBack 阅读进度条上的进度,但运气不佳。关于如何获取该信息以便 TalkBack 可以说出来的任何想法。 谢谢 公共(public)类 MainActivity 扩展 Activi
进度条总是返回null public void calcola(View view) { final Dialog myDialog = new Dialog(this);
我正在尝试用选取框样式测试进度条,但没有成功 private void Form1_Load(object sender, EventArgs e) { Applica
我使用以下 Ruby 片段下载 a 8.9MB file . require 'open-uri' require 'net/http' require 'uri' def http_download
我为操作栏按钮尝试了两种不同的进度条样式,通过 refreshMenuItem.setActionView(R.layout.actionbar_indeterminate_progress); ac
每当我输入 fragment 时,我都会看到几秒钟的空白,然后 WebView 中的网页就会出现。 ProgressBar 好像没有显示。 这是我的 XML 文件,用于 WebView 和 Progr
我的包依赖于一个名为 progressbar 的包。有一个bug in progressbar 2.3导致Python>3.3安装失败,因此我安装了latest git repo from this
任何默认的 ProgressBar 都有一个 ClipDrawable 级别,例如,一个绿色的 Drawable 在我的设备上填充灰色背景,在其他设备上填充黄色。 我的问题:当进度小于最大值时,是否可
这是随意的原型(prototype)代码,因此我尝试了我认为应该有效的方法,如果没有,则在谷歌上搜索,然后在仔细阅读类似问题后在这里提问。 我的 Shell 中有以下标记看法:
要在执行查询时显示 ProgressBar,我使用以下代码: if not Query1.Prepared then Query1.Prepare; Query1.Open; ProgressBa
我有三个类,例如 View 、逻辑、 Controller 。 class View{ Progressbutton pb; void start animation(){
我正在用 C#/WPF(.Net 框架 4.0)编写一个应用程序。我遇到的一件事是如何在应用程序非常繁忙时更新控件(如果可能)。我想我只是在努力尝试我在网上找到的东西来让它工作(而且在极少数情况下它会
要在执行查询时显示 ProgressBar,我使用以下代码: if not Query1.Prepared then Query1.Prepare; Query1.Open; ProgressBa
已关闭。此问题不符合 Stack Overflow guidelines 。目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 已关闭 3 年前。 奥 git _a
我创建了一个 ping 程序来 ping 5 服务器。如何在进度运行时让进度条运行。当 ping 完成时,进度条将为 100% private void btnGetstatusActionPer
我想在流程完成后在 PrimeFaces 上隐藏一个进度条。我认为没有必要在不工作时显示进度条。 html代码:
我想添加JProgressBar,它将在下载文件的程序中填充。 第三个问题是如何使进度条在 setValue 中添加正确的金额(例如:当它下载最后一个文件时,JPB 将完成)。 请注意,要下载的文件数
给定一个变量 N,反射(reflect)秒数,我想要一个 jQueryUI Progressbar顺利地从空更新到反射(reflect)该时间段内的完成。 换句话说,我想要一个显示进度条的functi
我是一名优秀的程序员,十分优秀!