Android 如何按百分比显示进度条状态
我正在使用android 2.3.3的进度对话框。进度对话框的状态显示为“60%60/100”,但我只想显示百分比,而不是“60/100”。我怎么做?请帮忙 请访问链接 在SDK中的ApiDemos中也对其进行了完整的解释。您想要的示例名为:ProgressBar3.java,可以在\ApiDemos\src\com\example\android\api\view中找到\Android 如何按百分比显示进度条状态,android,android-progressbar,Android,Android Progressbar,我正在使用android 2.3.3的进度对话框。进度对话框的状态显示为“60%60/100”,但我只想显示百分比,而不是“60/100”。我怎么做?请帮忙 请访问链接 在SDK中的ApiDemos中也对其进行了完整的解释。您想要的示例名为:ProgressBar3.java,可以在\ApiDemos\src\com\example\android\api\view中找到\ 此外,如果要删除对话框的边框,以便只显示进度条,则可以将对话框本身定义为透明视图/覆盖(示例中也有说明)。根据,应调用se
此外,如果要删除对话框的边框,以便只显示进度条,则可以将对话框本身定义为透明视图/覆盖(示例中也有说明)。根据,应调用
setProgressNumberFormat(null)
在您的ProgressDialog
实例上获取此行为嘿,这里是从服务器下载文件,将文件保存到SD卡并显示进度条
public class XYZ extends Activity {
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private Button startBtn;
private ProgressDialog mProgressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startBtn = (Button)findViewById(R.id.startBtn);
startBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
startDownload();
}
});
}
private void startDownload() {
String url = "http://.jpg";
new DownloadFileAsync().execute(url);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/some_photo_from_gdansk_poland.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
}
公共类XYZ扩展活动{
公共静态最终整型对话框\u下载\u进度=0;
专用按钮startBtn;
private ProgressDialog mProgressDialog;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startBtn=(按钮)findViewById(R.id.startBtn);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
startDownload();
}
});
}
私有void startDownload(){
字符串url=”http://.jpg";
新建DownloadFileAsync().execute(url);
}
@凌驾
受保护的对话框onCreateDialog(int id){
开关(id){
案例对话框\u下载\u进度:
mProgressDialog=新建进度对话框(此);
设置消息(“下载文件…”);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_水平);
mProgressDialog.setCancelable(假);
mProgressDialog.show();
返回mProgressDialog;
违约:
返回null;
}
}
类DownloadFileAsync扩展AsyncTask{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
showDialog(对话框下载进度);
}
@凌驾
受保护的字符串背景(字符串…aurl){
整数计数;
试一试{
URL=新URL(aurl[0]);
URLConnection conexion=url.openConnection();
conexion.connect();
int lenghtOfFile=conexion.getContentLength();
Log.d(“ANDRO_ASYNC”,“文件长度:“+lenghtOfFile”);
InputStream输入=新的BufferedInputStream(url.openStream());
OutputStream output=新文件OutputStream(“/sdcard/some_photo_from_gdansk_poland.jpg”);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
出版进度(“+(int)((总计*100)/长度文档));
输出.写入(数据,0,计数);
}
output.flush();
output.close();
input.close();
}捕获(例外e){}
返回null;
}
受保护的void onProgressUpdate(字符串…进度){
Log.d(“ANDRO_ASYNC”,progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@凌驾
受保护的void onPostExecute(字符串未使用){
dismissDialog(对话框下载进度);
}
}
}
像这样你会得到
这是我在网上找到的示例图像。
这是我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:text="Start long running task.."
android:id="@+id/startBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
您提到的方法适用于api 11级。我的10米坏:(我已经深入研究了源代码,发现在api 10中没有“正常”的方法来实现它。我可以建议作为一种解决办法,从Android 4.0.1中获取完整的ProgessDialog代码,并使用它来代替内置版本。也许,这会给Android“内部”带来一些麻烦资源,但它也可以作为可重用的源。这不是问题的答案