Android 将多个Seekbar限制在最大100%的总体范围内
我有5个seekbar,我希望用户能够操纵它们。seekbar相互依赖,因此如果一个seekbar设置为100%,则所有其他seekbar都必须为0%。我已经能够使用中的代码实现这一点,但是当操纵seekbar时,它会出现故障,seekbar会跳转。例如,默认情况下,我将所有seekbar设置为20%。为了将seekbar_1提高20%,您需要将另一个seekbar的值降低20%(例如seekbar_2)。这比使用seekbar_1可以释放20%的空间。我的问题是seekbar的实际触摸和移动,我认为这是因为我在onProgressChanged方法中进行了各种计算。我在下面附上了我的代码。有没有更简单的方法来实现这一点?任何帮助都将不胜感激Android 将多个Seekbar限制在最大100%的总体范围内,android,progress-bar,seekbar,android-progressbar,Android,Progress Bar,Seekbar,Android Progressbar,我有5个seekbar,我希望用户能够操纵它们。seekbar相互依赖,因此如果一个seekbar设置为100%,则所有其他seekbar都必须为0%。我已经能够使用中的代码实现这一点,但是当操纵seekbar时,它会出现故障,seekbar会跳转。例如,默认情况下,我将所有seekbar设置为20%。为了将seekbar_1提高20%,您需要将另一个seekbar的值降低20%(例如seekbar_2)。这比使用seekbar_1可以释放20%的空间。我的问题是seekbar的实际触摸和移动,
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
}}
public类MainActivity扩展了活动在seekbarchanegListener上的实现{
公共SeekBar sb1、sb2、sb3、sb4、sb5;
公共文本视图tv1、tv2、tv3、tv4、tv5;
公共int MPercentremining、mPercentTotal、mPercentUsed;
公共国际货币百分比;
@凌驾
创建时的公共void(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.setonseekbarchaneglistener(此);
sb2.setonseekbarchaneglistener(此);
sb3.setonseekbarchaneglistener(本);
sb4.setonseekbarchaneglistener(本);
sb5.setonseekbarchaneglistener(本);
mPercentTotal=100;
mPercentUsed=100;//seekbar默认设置为20%
mPercentRemaining=mPercentTotal-mPercentUsed;
}
@凌驾
public void onProgressChanged(SeekBar-SeekBar、int-progress、boolean-fromUser)
{
开关(seekBar.getId())
{
案例R.id.seekBar1:
如果((进度-mCurrentPercent)=进度)
tv1.setText(进度);
其他的
{
seekBar.setProgress(mCurrentPercent);
tv1.setText(mCurrentPercent);
}
打破
案例R.id.seekBar2:
如果((进度-mCurrentPercent)=进度)
tv2.setText(进度);
其他的
{
seekBar.setProgress(mCurrentPercent);
tv2.setText(mCurrentPercent);
}
打破
案例R.id.seekBar3:
如果((进度-mCurrentPercent)=进度)
tv3.setText(进度);
其他的
{
seekBar.setProgress(mCurrentPercent);
tv3.setText(mCurrentPercent);
}
打破
案例R.id.seekBar4:
如果((进度-mCurrentPercent)=进度)
tv4.setText(进度);
其他的
{
seekBar.setProgress(mCurrentPercent);
tv4.setText(mCurrentPercent);
}
打破
案例R.id.seekBar5:
如果((进度-mCurrentPercent)=进度)
tv5.setText(进度);
其他的
{
seekBar.setProgress(mCurrentPercent);
tv5.setText(mCurrentPercent);
}
打破
}
mPercentUsed=sb1.getProgress()+sb2.getProgress()+sb3.getProgress()+sb4.getProgress()+sb5.getProgress();
mPercentRemaining=mPercentTotal-mPercentUsed;
}
@凌驾
开始跟踪触摸时的公共无效(请参见kbar arg0)
{
mCurrentProgress=seekBar.getProgress();
}
@凌驾
TopTrackingTouch上的公共无效(请参见kbar arg0)
{
//TODO自动生成的方法存根
}}
例如,默认情况下,我将所有seekbar设置为20%。为了
将seekbar_1提高20%,则会降低另一个seekbar的值(例如
seekbar_2下降20%)。这比使用seekbar_1可以释放20%的空间。
我的问题是seekbar的实际触摸和移动,它会跳跃
我想是因为我在
改变方法。我在下面附上了我的代码。有没有
更简单的实现方法
看看下面的代码,看看你是如何做你想做的事情的。有大量的文档记录,因此理解它不会有问题:
private static final int TOTAL_AMOUNT = 100; // the maximum amount for all SeekBars
// stores the current progress for the SeekBars(initially each SeekBar has a
// progress of 20)
private int[] mAllProgress = { 20, 20, 20, 20, 20 };
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// find out which SeekBar triggered the event so we can retrieve its saved current
// progress
int which = whichIsIt(seekBar.getId());
// the stored progress for this SeekBar
int storedProgress = mAllProgress[which];
// we basically have two cases, the user either goes to the left or to
// the right with the thumb. If he goes to the right we must check to
// see how much he's allowed to go in that direction(based on the other
// SeekBar values) and stop him if he the available progress was used. If
// he goes to the left use that progress as going back
// and freeing the track isn't a problem.
if (progress > storedProgress) {
// how much is currently available based on all SeekBar progress
int remaining = remaining();
// if there's no progress remaining then simply set the progress at
// the stored progress(so the user can't move the thumb further)
if (remaining == 0) {
seekBar.setProgress(storedProgress);
return;
} else {
// we still have some progress available so check that available
// progress and let the user move the thumb as long as the
// progress is at most as the sum between the stored progress
// and the maximum still available progress
if (storedProgress + remaining >= progress) {
mAllProgress[which] = progress;
} else {
// the current progress is bigger then the available
// progress so restrict the value
mAllProgress[which] = storedProgress + remaining;
}
}
} else if (progress <= storedProgress) {
// the user goes left so simply save the new progress(space will be
// available to other SeekBars)
mAllProgress[which] = progress;
}
}
/**
* Returns the still available progress after the difference between the
* maximum value(TOTAL_AMOUNT = 100) and the sum of the store progresses of
* all SeekBars.
*
* @return the available progress.
*/
private final int remaining() {
int remaining = TOTAL_AMOUNT;
for (int i = 0; i < 5; i++) {
remaining -= mAllProgress[i];
}
if (remaining >= 100) {
remaining = 100;
} else if (remaining <= 0) {
remaining = 0;
}
return remaining;
}
private int whichIsIt(int id) {
switch (id) {
case R.id.seekBar1:
return 0; // first position in mAllProgress
case R.id.seekBar2:
return 1;
case R.id.seekBar3:
return 2;
case R.id.seekBar4:
return 3;
case R.id.seekBar5:
return 4;
default:
throw new IllegalStateException(
"There should be a Seekbar with this id(" + id + ")!");
}
}
private static final int TOTAL_AMOUNT=100;//所有seekbar的最大值
//存储SeekBar的当前进度(最初每个SeekBar都有一个
//项目进展(20)
私有进程={20,20,20,20,20};
@凌驾
更改了公共进程无效(SeekBar SeekBar,int progress,
布尔值(用户){
//找出是哪个SeekBar触发了事件,以便我们可以检索其保存的当前事件
//进展
int-whichist(seekBar.getId());
//此SeekBar的存储进度
int storedProgress=mAllProgress[which];
//我们基本上有两种情况,用户要么向左,要么向右
//右手拿着大拇指。如果他向右走,我们必须检查一下
//看看他可以往那个方向走多少(基于另一个方向)
//如果他使用了可用的进度,则阻止他。如果
//他走到左边,用这个过程作为回去
//释放赛道也不是问题。
如果(进度>存储进度){
//根据所有SeekBar进度,目前有多少可用
int剩余=剩余();
//如果没有剩余的进度,只需将进度设置为
//存储的进度(因此用户无法进一步移动拇指)
如果(剩余==0){
seekBar.setProgress(storedProgress);
返回;
}否则{
//我们仍有一些进展,请检查是否可用
//继续并让用户移动拇指,只要
//进度最多为存储的进度之间的总和
//还有最大的可用进度
如果(存储进度+剩余时间)