带有小数值的SeekBar

9

我应该创建一个使用小数值的 SeekBar 吗?

例如,它会显示:

0.0, 0.1, 0.2, 0.3, ..., 5.5, 5.6, 5.7, ..., 9.9, 10.0

2个回答

15
SeekBar 默认值为 0 到 100。当从SeekBar的更改监听器调用 onProgressChanged 函数时,进度数字将传递到 progress 参数中。
如果您想将此进度从 0.0->10.0 转换为小数以显示或处理,您只需要在接收到进度值时将其除以 10,并将该值转换为浮点型。以下是一些示例代码:
aSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        float value = ((float)progress / 10.0);
        // value now holds the decimal value between 0.0 and 10.0 of the progress
        // Example:
        // If the progress changed to 45, value would now hold 4.5
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {}
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {}
});

3

SeekBar的进度是0到100之间的整数。如果需要其他值,可以对进度值进行适当的算术运算来缩放它。

在您的情况下,除以10就可以了。您的代码可能类似于这样:

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        float decimalProgress = (float) progress/10;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接