简体   繁体   中英

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. I have the maximum values of both these seekbars set to 10.

I will be using the value of the seekbars to alter 2 numbers in my code.

So basically, I have 2 variables that have a range. 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. I am taking the progressValue from the seekbar and converting that value to a percentage stored as a double, and then doing the equations

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. I tried setting a textview equal to the progress value and testing it at various postions, and the progressValue is indeed working correctly. But for some reason these mod values will not assume a value anywhere else between the range I'd like. 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. Casting 10 to a double should solve the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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