将多个搜索栏限制为最多100%的整体 [英] Limit multiple seekbars to a maximum of 100% overall

查看:56
本文介绍了将多个搜索栏限制为最多100%的整体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有5个搜索栏,希望用户能够操纵它们.搜寻栏相互依赖,因此如果将搜寻栏设置为100%,则其他所有栏都必须为0%.我已经能够使用此帖子中找到的代码来实现此功能,但是在操纵搜索栏时,它会出现故障并且搜索栏会跳转大约.例如,默认情况下,我将所有搜索栏设置为20%.为了将seekbar_1提高20%,您将另一个seekbar的值降低了(例如,seekbar_2降低了20%).这释放了seekbar_1可以使用的20%.我的问题是搜索栏的实际触碰和移动,由于我在onProgressChanged方法中进行了各种计算,因此它跳来跳去.我在下面附加了我的代码.有没有更简单的方法来实现这一目标?任何帮助将不胜感激.

I have 5 seekbars which I want the user to be able to manipulate. The seekbars are dependent on one another so if one is set to 100% all others would have to be 0%. I have been able to implement this using the code found in this post however when manipulating the seekbars it is glitchy and the seekbar jumps around. For example, by default I set all the seekbars to 20%. In order to move seekbar_1 20% higher, you lower the value of another seekbar (say seekbar_2 by 20%). This frees up 20% of which seekbar_1 can than use. My issue is with the actual touch and move of the seekbar, it jumps around I think because of the various calculations I do within the onProgressChanged method. I've attached my code below. Is there an easier way to implement this? Any help would be much appreciated.

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
}}

推荐答案

例如,默认情况下,我将所有搜索栏设置为20%.为了 将seekbar_1提高20%,则降低另一个seekbar的价值(例如 seekbar_2降低了20%).这释放了seekbar_1可以使用的20%. 我的问题是搜寻杆的实际触碰和移动,它会跳 我认为,由于我在内部进行了各种计算, onProgressChanged方法.我在下面附加了我的代码.有没有 更简单的方法来实现这一点?

For example, by default I set all the seekbars to 20%. In order to move seekbar_1 20% higher, you lower the value of another seekbar (say seekbar_2 by 20%). This frees up 20% of which seekbar_1 can than use. My issue is with the actual touch and move of the seekbar, it jumps around I think because of the various calculations I do within the onProgressChanged method. I've attached my code below. Is there an easier way to implement this?

查看下面的代码,了解如何执行所需的操作.有大量文献记录,因此理解它应该没有问题:

Have a look at the code below on how you might do what you want. Is heavily documented so there shouldn't be a problem understanding it:

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 + ")!");
        }
    }

这篇关于将多个搜索栏限制为最多100%的整体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆