问题
I want to start My seek baar from base 3 and increment by 2 and max is 9 how to set , i need 3,5,7,9,11. my code is
it not working as i want , please help
SeekBar sb1 = (SeekBar)findViewById(R.id.sBtaxitype);
sb1.setMax(11);
sb1.setLeft(3);
sb1.incrementProgressBy(2);
sb1.setProgress(0); // Set it to zero so it will start at the left-most edge
sb1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress < 3) {
progress = 3;
progress = progress + 2;
} else {
progress = progress + 2; // Add the minimum value (10)
}
TextView text = (TextView)findViewById(R.id.tvTaxitype);
text.setText(Integer.toString(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
回答1:
setLeft()
is for positioning. A SeekBar
always starts with a value of 0 and rises to the maximum (100 by default, overridden via setMax()
).
To get a 3-11 range, call setMax(8)
, then add 3 to the result of getProgress()
when you retrieve the current value.
回答2:
To move your SeekBar in increments you test for fromUser
and set the progress to the value you want.
In this example the progress indicator will "snap" to the nearest 10 as you move it.
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
// round progress up or down to the nearest 10
int roundedProgress = (int) (Math.rint((double) progress / 10) * 10);
if (fromUser)
{
seekBar.setProgress(roundedProgress);
}
}
来源:https://stackoverflow.com/questions/19383246/set-custom-values-to-seek-bar