如何仅以恒定间隔停止搜索条的进度

时间:2016-12-02 06:33:21

标签: android seekbar

我有一个搜索栏,范围从0到100。我想在搜索栏中创建离散点,使得进度仅在0,10,20停止等等。我不能在中间停止。 我尝试使用以下代码,但我无法成功。

 seekBar.setProgress(0);
    //seekBar.incrementProgressBy(10);
    seekBar.setMax(100);

 seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {                   progress = progress / 10;
            progress = progress * 10;
            seekBarValue.setText(String.valueOf(progress));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {


        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            int seekBarIntValue;
        //    seekBar.incrementProgressBy(10);
            if((seekBar.getProgress()%10)!=0){
                if((seekBar.getProgress()%10)> 5)
                {
                     seekBarIntValue =(seekBar.getProgress()/10)+(seekBar.getProgress()%10);

                }else{
                     seekBarIntValue =(seekBar.getProgress()/10)-(seekBar.getProgress()%10);
                }

            }else{
                 seekBarIntValue = (seekBar.getProgress());
            }
            seekBar.setProgress(seekBarIntValue);

        }
    });

请帮我解决这个问题。

4 个答案:

答案 0 :(得分:1)

试试这个。

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
  // round progress up or down to the nearest 10
  int roundedProgress = (int) (Math.rint((double) progress / 10) * 10);
  if (fromUser)
  {
    seekBar.setProgress(roundedProgress);
  }
}

答案 1 :(得分:0)

为此您需要覆盖onProgressChanged方法。 我是这样做的。

@Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        int interval = 20;

        progress = (progress/interval)*interval;
        seekBar.setProgress(progress);

    }

答案 2 :(得分:0)

试试以下代码

 seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        int progress = 0;

        @Override
        public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
            progress = progresValue;
           seekBarValue.setText(progress + "/" + seekBar.getMax());
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // Do something here, if you want to do anything at the start of
            // touching the seekbar
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // Display the value in textview

        }
    });

答案 3 :(得分:0)

示例强制SeekBar停在30

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if(progress > 30){
            seekBar.setProgress(30);
            return;
        }
        tvProgress.setText(""+progress);
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
    }
});