带有十进制值的 SeekBar [英] SeekBar with decimal values

查看:52
本文介绍了带有十进制值的 SeekBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否创建了一个使用十进制值的创建 SeekBar?

Do I create a create SeekBar that uses decimal values?

例如,它会显示

0.0, 0.1, 0.2, 0.3, ..., 5.5>、5.65.7、...、9.910.0

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

推荐答案

SeekBar 默认为 0 到 100 之间的值.当从 SeekBar 的更改侦听器调用 onProgressChanged 函数时,进度编号在 progress 参数中传递.

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.

如果您想将此进度转换为从 0.0 -> 10.0 的小数以显示或处理,您需要做的就是在收到进度值时将进度除以 10,然后将该值转换为浮点数.下面是一些示例代码:

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

这篇关于带有十进制值的 SeekBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆