-4

您好,我在 android 中有一个最大进度值为 100 的搜索栏。我希望搜索栏只能在 0-100 之间上升到 50。它不允许进度超过 50。我的逻辑是,我不想更改搜索栏进度的最大值。

请帮助我如何在 android 的 seekbar 中做到这一点。提前致谢。

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

2 回答 2

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

        }
    });
于 2012-07-19T08:00:01.653 回答
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" />

于 2012-07-19T11:07:27.143 回答