【问题标题】:Android percentage ProgressBarAndroid 百分比进度条
【发布时间】:2013-12-13 21:43:45
【问题描述】:

在我的 ProgressBar 中,我想以 0.0 格式显示进度百分比。我有这段代码,但结果总是向上或向下四舍五入,不显示小数点后的数字。

double value = Double.valueOf(data);
value = numero_a / numero_b *100;
final float numero_float = (float) value;
new Thread(new Runnable() {
    float progressStatus = 0;
    Handler handler = new Handler();
    public void run() {
        while (progressStatus < numero_float) {      
            progressStatus += 1;
            handler.post(new Runnable() {
                public void run() {
                    tvPercentuale.setText(progressStatus+"%");
                    mProgress.setProgress((int)progressStatus);

【问题讨论】:

    标签: android view numbers


    【解决方案1】:

    将变量增加 10 倍,所以:

    while(progressStatus < numero_float * 10)
    

    然后将progressStatus 增加10:

    progressStatus += 10;
    

    然后将文本设置为:

    tvPercentuale.setText("" + progressStatus / 10.0f + "%");
    mProgress.setProgress((int)(progressStatus/10));
    

    【讨论】:

    • 同样的结果。四舍五入的结果(例如 48.0%)应该是准确的,即 47.8%。可能不支持小数?
    【解决方案2】:
        NumberFormat nf = NumberFormat.getPercentInstance();
        //this will make sure the format is in XX.X%
        nf.setMaximumFractionDigits(1);
        nf.setMinimumFractionDigits(1);
        .....
        tvPercentuale.setText(nf.format(progressStatus))
    

    编辑:

    int max = mProgress.getMax();
    
    for(int i=0; i<max; i++){
     int progress = i;
     float progressValue = (float)i/(float)max;
     tvPercentuale.setText(nf.format(progressValue))
     mProgress.setProgress(progress);
    }
    

    【讨论】:

    • 问题是即使如此,该值也是四舍五入的。我想看到 47.8%。但现在我看到了 48.0%
    • 那么progressStatus 会预先四舍五入。也许你需要做:float progressStatus = 0F; .... progressStatus += 1F;
    • 问题是setProgress 不接受浮点数据而只接受整数。有没有办法接受浮动?
    • 我认为你的逻辑有点倒退,这是我的做法:
    猜你喜欢
    • 2012-12-23
    • 2015-06-03
    • 2010-11-22
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    相关资源
    最近更新 更多