SeekBar with decimal values

a 夏天 提交于 2019-12-10 02:58:48

问题


Do I create a create SeekBar that uses decimal values?

For example, it would display

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


回答1:


A SeekBar defaults to a value between 0 and 100. When the onProgressChanged function is called from the SeekBar's change listener, the progress number is passed in the progress parameter.

If you wanted to convert this progress into a decimal from 0.0 -> 10.0 to display or process, all you would need to do is divide the progress by 10 when you receive a progress value, and cast that value into a float. Here's some example code:

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



回答2:


The progress of a SeekBar is an int between 0 and 100. Perform suitable arithmetic operation on the progress value to scale it if you need other values.

In your case division by 10 will do the trick. Something like this in your code:

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


来源:https://stackoverflow.com/questions/6197674/seekbar-with-decimal-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!