如何将 setText 设置为所有 SeekBar 进度值的总和?
How to setText to total of all SeekBar progress values?
我想"result.setText"显示所有SeekBars累计进度的变化总和。我还试图将变化的 int 存储到它自己的变量中(等于所有 SeekBar 进度的总和。)
我有 9 个 SeekBar,它们最多有 3 个。当我更改其中任何一个时,我希望 totalScoreInt 等于 +1。如果我将每个 SeekBar 从 0 向上移动一个,一次一个,我希望 TotalScoreInt 增加 1,从 0 到 27,如果我对每个 SeekBar 做相反的操作,TotalScoreInt 将从 27 减少 1 到 0在他们的最大值。
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBar1value = progress;
seekBar2value = progress;
seekBar3value = progress;
seekBar4value = progress;
seekBar5value = progress;
seekBar6value = progress;
seekBar7value = progress;
seekBar8value = progress;
seekBar9value = progress;
totalScoreInt =
(seekBar1value
+ seekBar2value
+ seekBar3value
+ seekBar4value
+ seekBar5value
+ seekBar6value
+ seekBar7value
+ seekBar8value
+ seekBar9value);
switch (seekBar.getId()){
case R.id.seekBar1:
result.setText("Score: " +progress);
break;
case R.id.seekBar2:
result.setText("Score: " +progress);
break;
case R.id.seekBar3:
result.setText("Score: " +progress);
break;
case R.id.seekBar4:
result.setText("Score: " +progress);
break;
case R.id.seekBar5:
result.setText("Score: " +progress);
break;
case R.id.seekBar6:
result.setText("Score: " +progress);
break;
case R.id.seekBar7:
result.setText("Score: " +progress);
break;
case R.id.seekBar8:
result.setText("Score: " +progress);
break;
case R.id.seekBar9:
result.setText("Score: " +progress);
break;
}
//result.setText(String.valueOf("Score: " +totalScoreInt));
}
此代码将文本设置为上次更改的 SeekBar 进度值所在的位置。
我希望每次更改任何 SeekBars 进度时该数字都会增加或减少。
所以如果 seekBar1 progress = 3 和 seekBar2 progress = 2 totalScoreInt 将等于 5 并且将在 "result"
中设置
您必须为每个 SeekBar
以及您的计数器变量保留一个全局引用。然后,创建一个有点像这样的方法:
private int progress;
private SeekBar sb1, sb2, sb3; // And the others...
private synchronized void handleSeekBars() {
progress = sb1.getProgress() + sb2.getProgress() + sb3.getProgress();
}
然后,在每个 SeekBar#OnSeekBarChangeListener()
中调用它,如下所示:
sb1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
handleSeekBars();
result.setText("Score: " + progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onStopTrackingTouch(SeekBar seekBar) { }
});
这应该可以解决问题。
我想"result.setText"显示所有SeekBars累计进度的变化总和。我还试图将变化的 int 存储到它自己的变量中(等于所有 SeekBar 进度的总和。)
我有 9 个 SeekBar,它们最多有 3 个。当我更改其中任何一个时,我希望 totalScoreInt 等于 +1。如果我将每个 SeekBar 从 0 向上移动一个,一次一个,我希望 TotalScoreInt 增加 1,从 0 到 27,如果我对每个 SeekBar 做相反的操作,TotalScoreInt 将从 27 减少 1 到 0在他们的最大值。
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBar1value = progress;
seekBar2value = progress;
seekBar3value = progress;
seekBar4value = progress;
seekBar5value = progress;
seekBar6value = progress;
seekBar7value = progress;
seekBar8value = progress;
seekBar9value = progress;
totalScoreInt =
(seekBar1value
+ seekBar2value
+ seekBar3value
+ seekBar4value
+ seekBar5value
+ seekBar6value
+ seekBar7value
+ seekBar8value
+ seekBar9value);
switch (seekBar.getId()){
case R.id.seekBar1:
result.setText("Score: " +progress);
break;
case R.id.seekBar2:
result.setText("Score: " +progress);
break;
case R.id.seekBar3:
result.setText("Score: " +progress);
break;
case R.id.seekBar4:
result.setText("Score: " +progress);
break;
case R.id.seekBar5:
result.setText("Score: " +progress);
break;
case R.id.seekBar6:
result.setText("Score: " +progress);
break;
case R.id.seekBar7:
result.setText("Score: " +progress);
break;
case R.id.seekBar8:
result.setText("Score: " +progress);
break;
case R.id.seekBar9:
result.setText("Score: " +progress);
break;
}
//result.setText(String.valueOf("Score: " +totalScoreInt));
}
此代码将文本设置为上次更改的 SeekBar 进度值所在的位置。
我希望每次更改任何 SeekBars 进度时该数字都会增加或减少。
所以如果 seekBar1 progress = 3 和 seekBar2 progress = 2 totalScoreInt 将等于 5 并且将在 "result"
中设置您必须为每个 SeekBar
以及您的计数器变量保留一个全局引用。然后,创建一个有点像这样的方法:
private int progress;
private SeekBar sb1, sb2, sb3; // And the others...
private synchronized void handleSeekBars() {
progress = sb1.getProgress() + sb2.getProgress() + sb3.getProgress();
}
然后,在每个 SeekBar#OnSeekBarChangeListener()
中调用它,如下所示:
sb1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
handleSeekBars();
result.setText("Score: " + progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onStopTrackingTouch(SeekBar seekBar) { }
});
这应该可以解决问题。