本文介绍了Android 用十进制设置搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个搜索栏,我想显示或设置我从 SharedPreferences 字符串中获得的值,但由于我不能使用小数,我不是 100% 确定如何执行此操作,有人可以指点我吗正确的方向?

So I have a seekbar that I want to display or setProgress the value of that I get from SharedPreferences String, but since I can't use decimals, I am not 100% sure how to do this, can someone point me in the right direction?

String BarValue = "3.5";

Bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        float value = 0;

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

        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
            SharedPreferences.Editor e = sp.edit();
            e.putString("barValue", String.valueOf(value));
            e.apply();
        }
    });

推荐答案

Android Seekbar 不支持浮点数,所以在 Seekbar 中使用整数,然后将其转换为浮点数想.我认为这是最好的方法.

Android Seekbar does not support float, so use integer in Seekbar and convert that to float as you want. I think that's the best way.

例如,如果您想使用 11.6 这样的单个小数位值,您可以将 116(11.6 * 10) 设置为 Seekbar,并在检索时 divide那个由 10.它会完成这项工作.

For example if you want to use single decimal place values like 11.6 you can set 116(11.6 * 10) to the Seekbar, and on retrieval divide that by 10. It will do the job.

这篇关于Android 用十进制设置搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 08:09