Android 如何限制搜索栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3490951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:26:57  来源:igfitidea点击:

how to limit seekbar

androidandroid-sdk-2.1seekbar

提问by Shishir.bobby

I'd like to set max and minimum limits of SeekBarto 50 and 20 respectively.

我想将SeekBar 的最大和最小限制分别设置为 50 和 20。

SeekBarhas a direct option top provide max value, but how to set its minimum value to 20 rather than 0?

SeekBar有一个直接选项 top 提供最大值,但如何将其最小值设置为 20 而不是 0?

回答by Paresh Mayani

In SeekBar you can set only max value.

在 SeekBar 你 can set only max value.

<SeekBar android:id="@+id/SeekBar01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="50"/>

And, You cannot directly set the minimum value to the seekbar.

并且,您不能直接为搜索栏设置最小值。

 SeekBar mSeekbar = (SeekBar) findViewById(R.id.SeekBar01);

    mSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
    {
       public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
       {
            length_edit.setText(Integer.toString(progress + 20));
       }

      public void onStartTrackingTouch(SeekBar seekBar) {}

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

As you see min progress you can sum with min which I would like - in your case 20.

当您看到 min 进度时,您可以用我想要的 min 求和 - 在您的情况下为 20。

回答by Romain Guy

Set the max value to 30 and add 20 to the values you read from the SeekBar.

将最大值设置为 30,并将 20 添加到您从 SeekBar 读取的值。

回答by Oleksandr Nos

The simplest way to do this is to just limit it in the SeekBar.OnSeekBarChangeListener()for your SeekBar.

最简单的方法是将它限制在SeekBar.OnSeekBarChangeListener()您的SeekBar.

First, you need to create field with min or default value. Then set this value when creating SeekBar:

首先,您需要创建具有最小值或默认值的字段。然后在创建时设置此值SeekBar

private int fillDefault;

In onCreateView()of a Fragmentor onCreateof an Activity:

onCreateView()aFragment或aonCreateActivity

fillDefault = 20;
fillSeekBar.setProgress(fillDefault);

Second, implement the listener for it:

其次,为它实现监听器:

fillSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            // Notification that the progress level has changed.
            if (progress < fillDefault){
                seekBar.setProgress(fillDefault); // magic solution, ha
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // Notification that the user has started a touch gesture.
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // Notification that the user has finished a touch gesture.
        }
    });

It works. Simple and awesome:D

有用。简单又棒:D

回答by Günay Gültekin

For example;

例如;

 public static int LENGTH_MIN_VALUE = 20;
 public static int LENGTH_MAX_VALUE = 50;
 SeekBar seekBar = (SeekBar) findViewById(R.id.passwordSeekBar);
    seekBar.setMax(LENGTH_MAX_VALUE);
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if(progress <= Constract.PASSWORD_LENGTH_MIN_VALUE){
                progress = Constract.PASSWORD_LENGTH_MIN_VALUE + progress;
            }

            String numberAsString = String.valueOf(progress);
            seekNumberView.setText(numberAsString);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {}

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

回答by WannaGetHigh

I have answered this question here : How to set seekbar min and max value.

我在这里回答了这个问题:How to set seekbar min and max value

With simple math you can set the min value, the max value and even the step of your seekbar.

通过简单的数学运算,您可以设置搜索栏的最小值、最大值甚至步长。

Hope this helps !

希望这可以帮助 !

回答by Andrei Nae

You can try this:

你可以试试这个:

seekBar.setMax(max_value-min_value);