更改搜索栏中的步长值?

发布于 2024-12-03 04:55:45 字数 174 浏览 1 评论 0原文

我有一个搜索栏,在移动它时我想将值从 0 更改为 200。我有一个 TextView,在移动搜索栏时在其中显示这些值。但我不想拥有该区间的每个值(0,1,2,3,4,5...),而是想要 10,20,30...等等。所以当我移动搜索栏时,我想显示 10,20,30,40.... 到 200。有人能给我一个提示或示例如何做到这一点吗?

I have a seekbar, while moving it I want to change values from 0 to 200. I have a TextView, where I display those values while moving the seekbar. But I don't want to have every value from that interval(0,1,2,3,4,5...), but to have 10, 20, 30...so on. So when I move the seekbar, I want to display 10,20,30,40....to 200. Can somebody give me a hint or an example how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

帥小哥 2024-12-10 04:55:45

尝试下面的代码

SeekBar seekBar = (SeekBar)layout.findViewById(R.id.seekbar);
seekBar.setProgress(0);
seekBar.incrementProgressBy(10);
seekBar.setMax(200);
TextView seekBarValue = (TextView)layout.findViewById(R.id.seekbarvalue);
seekBarValue.setText(tvRadius.getText().toString().trim());

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) {

    }
});

setProgress(int) 用于设置搜索栏的起始值

setMax(int) 用于设置搜索栏的最大值

如果你想设置边界然后您可以在 onProgressChanged 方法中检查进度条值。如果进度小于或大于边界,则您可以将进度设置为您定义的边界。

Try below code

SeekBar seekBar = (SeekBar)layout.findViewById(R.id.seekbar);
seekBar.setProgress(0);
seekBar.incrementProgressBy(10);
seekBar.setMax(200);
TextView seekBarValue = (TextView)layout.findViewById(R.id.seekbarvalue);
seekBarValue.setText(tvRadius.getText().toString().trim());

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) {

    }
});

setProgress(int) is used to set starting value of the seek bar

setMax(int) is used to set maximum value of seek bar

If you want to set boundaries of the seekbar then you can check the progressbar value in the onProgressChanged method. If the progress is less than or greater than the boundary then you can set the progress to the boundary you defined.

分开我的手 2024-12-10 04:55:45

您可以通过android:stepSize使用Material Components Library提供的Slider > 属性:

    <com.google.android.material.slider.Slider
        android:valueFrom="0"
        android:valueTo="200"
        android:stepSize="10"
       ..>

在此输入图像描述

You can use the Slider provided by the Material Components Library using the android:stepSize attribute:

    <com.google.android.material.slider.Slider
        android:valueFrom="0"
        android:valueTo="200"
        android:stepSize="10"
       ..>

enter image description here

給妳壹絲溫柔 2024-12-10 04:55:45

我能想到的最简单的方法就是简单地定义:

SeekBar yourSeekBar = (SeekBar)findViewById(R.id.yourSeekBarId);
yourSeekbar.setMax(20);

接下来,重写这些方法(空方法也是必需的,即使它们是空的):

yourSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {          
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (fromUser) {
                if (progress >= 0 && progress <= sizeSeekBar.getMax()) {                        

                    String progressString = String.valueOf(progress * 10);
                    yourTextView.setText(progressString); // the TextView Reference
                    seekBar.setSecondaryProgress(progress);
                }
            }

        }
    });

这个想法是只为搜索栏定义 20 个值,但总是相乘该值除 10 并显示该值。
如果您并不真正需要 200 个值,那么使用 200 个值就没有意义。

The easieset way I can think of, is simply defining:

SeekBar yourSeekBar = (SeekBar)findViewById(R.id.yourSeekBarId);
yourSeekbar.setMax(20);

Next, override those methods (the empty methods are also required, even if they are empty):

yourSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {          
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (fromUser) {
                if (progress >= 0 && progress <= sizeSeekBar.getMax()) {                        

                    String progressString = String.valueOf(progress * 10);
                    yourTextView.setText(progressString); // the TextView Reference
                    seekBar.setSecondaryProgress(progress);
                }
            }

        }
    });

The idea is to only define 20 values for the seekbar, but always multiply the value by 10 and display that value.
If you do not really need 200 values, then there is no point in using 200 values.

維他命╮ 2024-12-10 04:55:45

您应该使用 SeekBar.OnSeekBarChangeListener 来跟踪进度更改。调用mseek.setMax(200)。对当前值进行模运算来决定是否应该更新文本视图。

SeekBar.OnSeekBarChangeListener() {

    void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (progress % 10 == 0) {
            textview.setText(""+progress);
        }
    }
}

You should use the SeekBar.OnSeekBarChangeListener for tracking the progress change. Call mseek.setMax(200). Do a modulo operation on the current value to decide if the textview should be updated or not.

SeekBar.OnSeekBarChangeListener() {

    void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (progress % 10 == 0) {
            textview.setText(""+progress);
        }
    }
}
上课铃就是安魂曲 2024-12-10 04:55:45

您还可以通过定义自定义 SeekBar 类来实现此目的,如下所示,

public class CustomSeekBar extends AppCompatSeekBar {


int SEEKBAR_MULTIPLIER = 1;

public CustomSeekBar(Context context) {
    super(context);
}

public CustomSeekBar(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public void setSeekBarConfig( int SEEKBAR_MULTIPLIER, int SEEKBAR_MAX){
    setMax(SEEKBAR_MAX);
    this.SEEKBAR_MULTIPLIER = SEEKBAR_MULTIPLIER;
}

@Override
public int getProgress() {
    return super.getProgress() * SEEKBAR_MULTIPLIER;
}
}

并且可以通过以下方式使用

 CustomSeekBar mCustomSeekBar = (CustomSeekBar) findViewById(R.id.customSeekBar);
 mSeekBar.setSeekBarConfig(10,20);

在 xml 中,您应该添加 CustomSeekBar 而不是 SeekBar,

<*.CustomSeekBar
    com.quemb.qmbform.view:setSeekBarConfig="10"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/seekBar" />

这样您就可以在整个应用程序中重用自定义的eekBar,而无需重复整个额外的计算。

You can also achieve this by defining custom SeekBar class as below

public class CustomSeekBar extends AppCompatSeekBar {


int SEEKBAR_MULTIPLIER = 1;

public CustomSeekBar(Context context) {
    super(context);
}

public CustomSeekBar(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public void setSeekBarConfig( int SEEKBAR_MULTIPLIER, int SEEKBAR_MAX){
    setMax(SEEKBAR_MAX);
    this.SEEKBAR_MULTIPLIER = SEEKBAR_MULTIPLIER;
}

@Override
public int getProgress() {
    return super.getProgress() * SEEKBAR_MULTIPLIER;
}
}

And can use by following manner

 CustomSeekBar mCustomSeekBar = (CustomSeekBar) findViewById(R.id.customSeekBar);
 mSeekBar.setSeekBarConfig(10,20);

In xml you should add CustomSeekBar instead of SeekBar

<*.CustomSeekBar
    com.quemb.qmbform.view:setSeekBarConfig="10"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/seekBar" />

So that you can reuse the custom seekBar in entire App with out repeating whole extra calculation.

青春有你 2024-12-10 04:55:45

对于最小的问题,您可以设置进度补偿。这样,用户就不会低于你的最低值。

int offset = 10;

seekBar seekBar = (SeekBar)layout.findViewById(R.id.seekbar);
seekBar.setProgress(0);
seekBar.setMax(190); //with the offset, you need to adapt the max value
TextView seekBarValue = (TextView)layout.findViewById(R.id.seekbarvalue);
seekBarValue.setText(""+(seekBar.getProgress() + offset));

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        progress += offset;

        seekBarValue.setText(String.valueOf(progress));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
});

for the minimum issue, you can set an offset on your progress. This way, the user can't go under your minimal value.

int offset = 10;

seekBar seekBar = (SeekBar)layout.findViewById(R.id.seekbar);
seekBar.setProgress(0);
seekBar.setMax(190); //with the offset, you need to adapt the max value
TextView seekBarValue = (TextView)layout.findViewById(R.id.seekbarvalue);
seekBarValue.setText(""+(seekBar.getProgress() + offset));

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        progress += offset;

        seekBarValue.setText(String.valueOf(progress));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
});
何止钟意 2024-12-10 04:55:45

使用 Math.round 函数

fun round(n: Int): Int {
    // Smaller multiple
    val a = n / 10 * 10
    // Larger multiple
    val b = a + 10
    // Return of closest of two
    return if (n - a > b - n) b else a
}

use Math.round function

fun round(n: Int): Int {
    // Smaller multiple
    val a = n / 10 * 10
    // Larger multiple
    val b = a + 10
    // Return of closest of two
    return if (n - a > b - n) b else a
}
梦幻的心爱 2024-12-10 04:55:45

一种可能的解决方案是设置 seekBar.setMax(20) (或 XML 中的 android:max="20"),并且每当您使用或显示该值时,乘以移动 10。SeekBar

看起来一次至少移动 20。

One possible solution would be to set seekBar.setMax(20) (or android:max="20" in XML), and whenever you use or display the value, multiply it by 10.

The SeekBar would then appear to move at least 20 at a time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文