我有问题如何更改progressdialog中的文本(基本上如图所示具有STYLE_HORIZONTAL)(使用Android 1.6)
显示图中的文字。
请在这种情况下提供帮助。 我关于progressdialog的代码是这样的: -
mProgressDialog = new ProgressDialog(PDFActivity.this);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setTitle(R.string.msgDownloadingWait);
mProgressDialog.setMessage(getResources().getString(
R.string.msgDownloading));
// User is not allowed to cancel the download operation.
mProgressDialog.setCancelable(false);
mProgressDialog.setMax(serverFileCount);
mProgressDialog.show();
提前致谢。
答案 0 :(得分:2)
我在几天前得到了与这些东西相关的答案(但是今天更新了它,因为有一些空闲时间)。
这里是我用来制作这个东西最好的代码。我通过Custom Dialog实现了上述功能。首先,我在这里调用了自定义对话类的活动代码。
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ProgressThread extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyDialog dialog = new MyDialog(this);
dialog.show();
}
}
现在代码与自定义对话框相关。我在CustomDialog&中使用了ProgressBar & TextViews
。根据下载进行计算,然后更新TextView。此处使用的示例更新textviews&虚拟方式的进度条。您可以根据需要进行更改。
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MyDialog extends Dialog {
public static final int STATUS_UPDATE = 101;
public static final int STATUS_COMPLETE = 100;
ProgressBar progressBar;
TextView textView;
TextView percent;
int increment;
int progress;
public MyDialog(Context context) {
super(context);
setContentView(R.layout.progressbar);
setDialog();
}
private void setDialog() {
setTitle("Downloading Files....");
textView = (TextView) findViewById(R.id.textProgress);
progressBar = (ProgressBar) findViewById(R.id.progress_horizontal);
percent = (TextView) findViewById(R.id.textPercentage);
percent.setTextColor(Color.WHITE);
textView.setTextColor(Color.WHITE);
progressBar.setProgressDrawable(getContext().getResources()
.getDrawable(R.drawable.my_progress));
progressBar.setIndeterminate(false);
// set the maximum value
progressBar.setMax(1315);
launcherThread();
}
private void launcherThread() {
LoaderThread loaderThread = new LoaderThread();
loaderThread.start();
LauncherThread launcherThread = new LauncherThread();
launcherThread.start();
}
private class LoaderThread extends Thread {
@Override
public void run() {
try {
while (progressBar.getProgress() < progressBar.getMax()) {
// wait 500ms between each update
Thread.sleep(100);
increment++;
// active the update handler
progressHandler.sendEmptyMessage(STATUS_UPDATE);
}
progressHandler.sendEmptyMessage(STATUS_COMPLETE);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// handler for the background updating
Handler progressHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case STATUS_UPDATE:
progressBar.setProgress(increment);
float value = increment / 1315F;
percent.setText(" " + ((int) (value * 100)) + "%");
System.out.println(value * 100);
textView.setText(String.valueOf(progressBar.getProgress())
.concat(" / " + progressBar.getMax()));
break;
case STATUS_COMPLETE:
dismiss();
}
}
};
private class LauncherThread extends Thread {
@Override
public void run() {
progressHandler.sendMessage(progressHandler.obtainMessage());
progressHandler.sendEmptyMessage(0);
}
}
}