我有 5 个搜索栏,我希望用户能够操纵它们。搜索栏相互依赖,因此如果一个设置为 100%,所有其他搜索栏都必须为 0%。我已经能够使用这篇文章中的代码来实现这一点,但是在操作搜索栏时它会出现故障并且搜索栏会跳来跳去。例如,默认情况下,我将所有搜索栏设置为 20%。为了将 seekbar_1 提高 20%,您需要降低另一个 seekbar 的值(比如 seekbar_2 降低 20%)。这释放了 seekbar_1 可以使用的 20%。我的问题是搜索栏的实际触摸和移动,我认为它会跳来跳去,因为我在 onProgressChanged 方法中进行了各种计算。我在下面附上了我的代码。有没有更简单的方法来实现这个?任何帮助将非常感激。
public class MainActivity extends Activity implements OnSeekBarChangeListener{
public SeekBar sb1,sb2,sb3,sb4,sb5;
public TextView tv1,tv2,tv3,tv4,tv5;
public int mPercentRemaining, mPercentTotal, mPercentUsed;
public int mCurrentPercent;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sb1=(SeekBar) findViewById(R.id.seekBar1);
sb2=(SeekBar) findViewById(R.id.seekBar2);
sb3=(SeekBar) findViewById(R.id.seekBar3);
sb4=(SeekBar) findViewById(R.id.seekBar4);
sb5=(SeekBar) findViewById(R.id.seekBar5);
tv1=(TextView) findViewById(R.id.textView1);
tv2=(TextView) findViewById(R.id.textView2);
tv3=(TextView) findViewById(R.id.textView3);
tv4=(TextView) findViewById(R.id.textView4);
tv5=(TextView) findViewById(R.id.textView5);
sb1.setOnSeekBarChangeListener(this);
sb2.setOnSeekBarChangeListener(this);
sb3.setOnSeekBarChangeListener(this);
sb4.setOnSeekBarChangeListener(this);
sb5.setOnSeekBarChangeListener(this);
mPercentTotal = 100;
mPercentUsed = 100; //Seekbars are all set to 20% by default
mPercentRemaining = mPercentTotal - mPercentUsed;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
switch (seekBar.getId())
{
case R.id.seekBar1:
if((progress - mCurrentPercent) <= mPercentRemaining || mCurrentPercent >= progress)
tv1.setText(progress);
else
{
seekBar.setProgress(mCurrentPercent);
tv1.setText(mCurrentPercent);
}
break;
case R.id.seekBar2:
if((progress - mCurrentPercent) <= mPercentRemaining || mCurrentPercent >= progress)
tv2.setText(progress);
else
{
seekBar.setProgress(mCurrentPercent);
tv2.setText(mCurrentPercent);
}
break;
case R.id.seekBar3:
if((progress - mCurrentPercent) <= mPercentRemaining || mCurrentPercent >= progress)
tv3.setText(progress);
else
{
seekBar.setProgress(mCurrentPercent);
tv3.setText(mCurrentPercent);
}
break;
case R.id.seekBar4:
if((progress - mCurrentPercent) <= mPercentRemaining || mCurrentPercent >= progress)
tv4.setText(progress);
else
{
seekBar.setProgress(mCurrentPercent);
tv4.setText(mCurrentPercent);
}
break;
case R.id.seekBar5:
if((progress - mCurrentPercent) <= mPercentRemaining || mCurrentPercent >= progress)
tv5.setText(progress);
else
{
seekBar.setProgress(mCurrentPercent);
tv5.setText(mCurrentPercent);
}
break;
}
mPercentUsed = sb1.getProgress() + sb2.getProgress() + sb3.getProgress() + sb4.getProgress() + sb5.getProgress();
mPercentRemaining = mPercentTotal - mPercentUsed;
}
@Override
public void onStartTrackingTouch(SeekBar arg0)
{
mCurrentProgress = seekBar.getProgress();
}
@Override
public void onStopTrackingTouch(SeekBar arg0)
{
// TODO Auto-generated method stub
}}