【问题标题】:how to show progress bar status by percentage如何按百分比显示进度条状态
【发布时间】:2012-01-28 07:26:49
【问题描述】:

我正在使用 android 2.3.3 的进度对话框。进度对话框的状态以“60% 60/100”的格式显示,但我只想显示百分比而不是“60/100”。我该怎么做?请帮忙。

【问题讨论】:

    标签: android android-progressbar


    【解决方案1】:

    根据documentation,您应该在您的ProgressDialog 实例上调用setProgressNumberFormat (null) 以获取此行为

    【讨论】:

    • 你提到的方法适用于api级别11。我的是10
    • 我的错 :( 我已经深入研究了源代码,发现在 api 10 中没有“正常”的方法可以做到这一点。我只能提出从 Android 获取完整 ProgessDialog 代码的解决方法4.0.1 并使用它而不是内置版本。也许它会与android“内部”资源有些混乱,但它也可以作为资源来重用。
    【解决方案2】:

    Hey Here 从服务器下载文件,将文件保存到 sdcard 并显示进度条。

    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);
        }
    }
    }
    

    这样你就得到了..

    这是我在网上找到的示例图像。

    这是我的 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>
    

    【讨论】:

    • 这不是问题的答案
    【解决方案3】:

    请访问this链接。

    SDK 内的 ApiDemos 中也有完整的解释。您想要的示例名为:ProgressBar3.java,可以在 \ApiDemos\src\com\example\android\apis\view\ 中找到

    此外,如果您想移除对话框的边框以便只显示进度条,您可以将对话框本身定义为透明视图/叠加层(示例中也有说明)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      相关资源
      最近更新 更多