Do I create a create SeekBar
that use开发者_StackOverflow社区s decimal values?
For example, it would display
0.0
, 0.1
, 0.2
, 0.3
, ..., 5.5
, 5.6
, 5.7
, ..., 9.9
, 10.0
A SeekBar defaults to a value between 0 and 100. When the onProgressChanged
function is called from the SeekBar's change listener, the progress number is passed in the progress
parameter.
If you wanted to convert this progress into a decimal from 0.0 -> 10.0 to display or process, all you would need to do is divide the progress by 10 when you receive a progress value, and cast that value into a float. Here's some example code:
aSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float value = ((float)progress / 10.0);
// value now holds the decimal value between 0.0 and 10.0 of the progress
// Example:
// If the progress changed to 45, value would now hold 4.5
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
The progress of a SeekBar is an int between 0 and 100. Perform suitable arithmetic operation on the progress value to scale it if you need other values.
In your case division by 10 will do the trick. Something like this in your code:
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float decimalProgress = (float) progress/10;
}
精彩评论