Android Seekbar float values from 0.0 to 2.0

有些话、适合烂在心里 提交于 2019-12-03 06:30:51

SeekBar extends ProgressBar.

ProgressBar has a method

public void setMax(int max)

Since it only accepts int you'll have to do some conversion from an int value to get the float that you are after.

Something like this should do the trick:

mSeekBar.setMax(4);
//max = 4 so the possible values are 0,1,2,3,4
seekbar.setOnSeekBarChangeListener( new OnSeekBarChangeListener(){
    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
    {
        // TODO Auto-generated method stub
        Toast.makeText(seekBar.getContext(), "Value: "+getConvertedValue(progress), Toast.LENGTH_SHORT).show();
    }
    public void onStartTrackingTouch(SeekBar seekBar)
    {
        // TODO Auto-generated method stub
    }
    public void onStopTrackingTouch(SeekBar seekBar)
    {
        // TODO Auto-generated method stub
    }
});

This part will be in your onCreate() and you'll have to use findViewById() or something to get a reference to mSeekBar.

public float getConvertedValue(int intVal){
    float floatVal = 0.0;
    floatVal = .5f * intVal;
    return floatVal;
}

This part is another method that you can add to your Activity that will convert from int to float values within the range you need.

     textView.setProgress(0);
     textView.incrementProgressBy(1);
     textView.setMax(4);

       public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) { 

                       float progressD=(float) progress/2;
                       textView.setText(String.valueOf(progressD));

// from 0 to 2 step 0.5

Kirill Kulakov

You should implement your own custom seek bar, make it jump between the desired values and stick to them, you could find useful info here:

Making A Custom Skinny ProgressBar / Seekbar

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