Seekbar进度级别达到0到最大值之间的限制

时间:2012-07-19 06:19:31

标签: android seekbar

您好,我在android中有一个搜索栏,最大进度值为100。我希望搜索栏只能在0到100之间达到50。它不会允许进展beyound值50.我的逻辑就像我不想改变seekbar的最大进展值。

请帮助我如何在android中的搜索栏中执行此操作。提前谢谢。

@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);

    tv1=(TextView) findViewById(R.id.textView1);
    tv2=(TextView) findViewById(R.id.textView2);
    tv3=(TextView) findViewById(R.id.textView3);

    tvremaining=(TextView) findViewById(R.id.textView4);
    tvused=(TextView) findViewById(R.id.textView5);

    sb1.setOnSeekBarChangeListener(this);
    sb2.setOnSeekBarChangeListener(this);
    sb3.setOnSeekBarChangeListener(this);

    total=100;
    used=0;
    remaining=total-used;
    tvremaining.setText(Integer.toString(remaining));
    tvused.setText(Integer.toString(used));

}

@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) 
{
    switch (arg0.getId()) 
    {
    case R.id.seekBar1:
        if(arg2)
        {
            tv1.setText(Integer.toString(arg1));
            //arg0.setProgressDrawable(getResources().getDrawable(R.drawable.progress_normal));
        }
        break;
    case R.id.seekBar2:
        if(arg2)
        {
            tv2.setText(Integer.toString(arg1));
            //arg0.setProgressDrawable(getResources().getDrawable(R.drawable.progress_normal));
        }
        break;
    case R.id.seekBar3:
        if(arg2)
        {
            tv3.setText(Integer.toString(arg1));
            //arg0.setProgressDrawable(getResources().getDrawable(R.drawable.progress_normal));
        }
        break;
    }
    progress=arg1;
}

@Override
public void onStartTrackingTouch(SeekBar arg0) 
{
    //arg0.setMax(remaining);

    // TODO Auto-generated method stub
}

@Override
public void onStopTrackingTouch(SeekBar arg0) 
{
    switch (arg0.getId()) 
    {
    case R.id.seekBar1:
        //sb2.setProgressDrawable(getResources().getDrawable(R.drawable.progress_applied));
        //sb3.setProgressDrawable(getResources().getDrawable(R.drawable.progress_applied));

        sb2.setProgress(progress);
        sb3.setProgress(progress);
        break;
    case R.id.seekBar2:
        //sb1.setProgressDrawable(getResources().getDrawable(R.drawable.progress_applied));
        //sb3.setProgressDrawable(getResources().getDrawable(R.drawable.progress_applied));

        sb1.setProgress(progress);
        sb3.setProgress(progress);
        break;
    case R.id.seekBar3:
        //sb1.setProgressDrawable(getResources().getDrawable(R.drawable.progress_applied));
        //sb2.setProgressDrawable(getResources().getDrawable(R.drawable.progress_applied));

        sb1.setProgress(progress);
        sb2.setProgress(progress);
        break;
    }

    used= (Integer.parseInt(tv1.getText().toString()))+(Integer.parseInt(tv2.getText().toString()))+(Integer.parseInt(tv3.getText().toString()));
    remaining = total-used;
    tvremaining.setText(Integer.toString(remaining));
    tvused.setText(Integer.toString(used));

    // TODO Auto-generated method stub
}   

2 个答案:

答案 0 :(得分:5)

试试这个

final SeekBar mSeekBar=(SeekBar) findViewById(R.id.SeekBar);

    mSeekBar.setMax(100);
    mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            if(progress>=50)
            {
                mSeekBar.setProgress(50);
            }


            // TODO Auto-generated method stub

        }
    });

答案 1 :(得分:0)

我已经为逻辑实现了以下代码:

public class MainActivity extends Activity implements OnSeekBarChangeListener{
public SeekBar sb1,sb2,sb3;
public TextView tv1,tv2,tv3,tvremaining,tvused;
public int remaining, total,used;
public int progress;

@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);

    tv1=(TextView) findViewById(R.id.textView1);
    tv2=(TextView) findViewById(R.id.textView2);
    tv3=(TextView) findViewById(R.id.textView3);

    tvremaining=(TextView) findViewById(R.id.textView4);
    tvused=(TextView) findViewById(R.id.textView5);

    sb1.setOnSeekBarChangeListener(this);
    sb2.setOnSeekBarChangeListener(this);
    sb3.setOnSeekBarChangeListener(this);

    total=100;
    used=0;
    remaining=total-used;
    tvremaining.setText(Integer.toString(remaining));
    tvused.setText(Integer.toString(used));

}

@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) 
{
    switch (arg0.getId()) 
    {
    case R.id.seekBar1:
        if((arg1-progress)<=remaining || progress>=arg1)
            tv1.setText(Integer.toString(arg1));
        else
        {
            arg0.setProgress(progress);
            tv1.setText(Integer.toString(progress));
        }
        break;
    case R.id.seekBar2:
        if((arg1-progress)<=remaining || progress>arg1)
            tv2.setText(Integer.toString(arg1));
        else
        {
            arg0.setProgress(progress);
            tv2.setText(Integer.toString(progress));
        }
        break;
    case R.id.seekBar3:
        if((arg1-progress)<=remaining || progress>arg1)
            tv3.setText(Integer.toString(arg1));
        else
        {
            arg0.setProgress(progress);
            tv3.setText(Integer.toString(progress));
        }
        break;
    }

    used= (Integer.parseInt(tv1.getText().toString()))+(Integer.parseInt(tv2.getText().toString()))+(Integer.parseInt(tv3.getText().toString()));
    remaining = total-used;
    tvremaining.setText(Integer.toString(remaining));
    tvused.setText(Integer.toString(used));
}

@Override
public void onStartTrackingTouch(SeekBar arg0) 
{
    progress= arg0.getProgress();
    //arg0.setMax(remaining);

    // TODO Auto-generated method stub
}

@Override
public void onStopTrackingTouch(SeekBar arg0) 
{

    // TODO Auto-generated method stub
} }

以下是布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"
    android:layout_margin="40dp" />
<SeekBar
    android:id="@+id/seekBar2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/seekBar1"
    android:layout_margin="40dp" />

<SeekBar
    android:id="@+id/seekBar3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/seekBar2"
    android:layout_margin="40dp"  />
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/seekBar1"
    android:text="0" />
<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/seekBar2"
    android:text="0" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/seekBar3"
    android:text="0" />
<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView3"
    android:layout_marginTop="108dp"
    android:text="TextView" />