简体   繁体   English

Android Java:正确使用搜索栏的progressValue

[英]Android java: Using a seekbar's progressValue correctly

I am currently using a couple seekbars in my application. 我目前在我的应用程序中使用几个搜寻栏。 There are 2 that are giving me trouble. 有2个给我带来麻烦。 I have the maximum values of both these seekbars set to 10. 我将这两个搜索栏的最大值都设置为10。

I will be using the value of the seekbars to alter 2 numbers in my code. 我将使用搜寻列的值更改代码中的2个数字。

So basically, I have 2 variables that have a range. 所以基本上,我有2个具有范围的变量。 One is protein mod, and can be anywhere from 0.8 to 1.0, and fatmod that can be anywhere in between 0.3 and 0.6. 一种是蛋白质mod,可以介于0.8到1.0之间,而脂肪mod可以介于0.3到0.6之间。 I am taking the progressValue from the seekbar and converting that value to a percentage stored as a double, and then doing the equations 我从seekbar获取progressValue并将该值转换为存储为双精度值的百分比,然后执行公式

proteinmod = proteinmod = 0.8+(progressValue/10)*0.2;
fatmod = 0.3+(progressValue/10)*0.3;

MY ISSUE: The seekbars are only semi-functional. 我的问题:搜寻杆仅是半功能的。 When all the way to the left, they are the minimum value of the range as expected - but remain that value until the seekbar is all the way maxed out, which then the proteinmod and fatmod are set to there max. 当一直向左移动时,它们是预期范围的最小值-但一直保持该值,直到搜寻栏完全最大化,然后将Proteinmod和Fatmod设置为最大值。 I tried setting a textview equal to the progress value and testing it at various postions, and the progressValue is indeed working correctly. 我尝试将textview设置为progress值,并在各种位置对其进行测试,并且progressValue确实可以正常工作。 But for some reason these mod values will not assume a value anywhere else between the range I'd like. 但是由于某些原因,这些mod值不会在我想要的范围之间的其他任何地方使用值。 Here's my code. 这是我的代码。 Thank you so much in advance! 提前非常感谢您!

double proteinmod = 0;
double fatmod = 0;

    seekbar2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
            proteinmod = 0.8+(progressValue/10)*0.2;
            tv6.setText(String.valueOf(proteinmod));
            updateOutput();
        }

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

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }
    });

    seekbar3.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
            fatmod = 0.3+(progressValue/10)*0.3;
            tv6.setText(String.valueOf(fatmod));
            updateOutput();
        }

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

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }
    });

I believe this should be all the code you need, but if you need other bits, please feel free to ask. 我相信这应该是您需要的所有代码,但是如果您需要其他代码,请随时提出。

Try the following: 请尝试以下操作:

proteinmod = 0.8+(progressValue/10d)*0.2;

and

fatmod = 0.3+(progressValue/10d)*0.3;

Since progressValue and 10 are both Integers the result of the division is an Integer. 由于progressValue和10均为Integers,因此除法结果为Integer。 Casting 10 to a double should solve the problem. 将10转换为两倍应该可以解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM