Android百分比ProgressBar

时间:2013-12-13 21:43:45

标签: android view numbers

在我的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);

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

将变量增加10倍,所以:

while(progressStatus < numero_float * 10)

然后将progressStatus增加10:

progressStatus += 10;

然后将文本设置为:

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