我有多个SeekBar
。我想检测每个SeekBar
progress
值的变化详情,以便计算/更新totalVal
。
更具体地说,我想检测所选SeekBar
值是向右移动还是向左移动(相对于之前的progress
级别),以及移动了多少步
mySeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progressVal, boolean fromUser) {
// perform calculations here
}
// ...
}
例如:
SeekBar
向右移动了一步。Seekbar
向左移动了5步。有人可以告诉我如何检测SeekBar
移动的方向,以及移动steps
的数量吗?如果可能的话。
答案 0 :(得分:2)
您可以保存最后一个搜索栏值。所以你可以检测到搜索栏的方向..
protected void onCreate(Bundle savedInstanceState) {
int lastSeekbarVal = 0;
mySeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progressVal, boolean fromUser) {
if (progressVal > lastSeekbarVal) {/*Seekbar moved to the right*/}
else if (progressVal < lastSeekbarVal) {/*Seekbar moved to the left*/}
lastSeekbarVal = progressVal;
}
// ...
}
}
答案 1 :(得分:-1)
有两个Integer类成员。他们的名字是seekBar1Current,seekBar2Current。
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progressVal, boolean fromUser) {
if(progressVal < seekBar1Current) {
totalVal = totalVal - (seekBar1Current - progressVal);
} else {
totalVal = totalVal + (progressVal - seekBar1Current);
}
seekBar1Current = totalVal;
}
// ...
});
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progressVal, boolean fromUser) {
if(progressVal < seekBar1Current) {
totalVal = totalVal - (seekBar2Current - progressVal);
} else {
totalVal = totalVal + (progressVal - seekBar2Current);
}
seekBar2Current = totalVal;
}
// ...
});
重要提示:seekBar1Current和seekBar2Current必须是CLASS MEMBER !!!