1

我正在尝试实现一个具有固定值的离散滑块,但我唯一可以设置的是 valueFrom、valueTo 和 stepSize。

这是我尝试做的代码

<com.google.android.material.slider.Slider
        android:id="@+id/slider"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="8dp"
        app:tickColor="@color/colorSecondaryLight"
        app:tickColorActive="@color/colorSecondary" />

有没有办法在滑块上设置固定值?(我将使用 2、5、10、25、50 和 100 的值)

4

3 回答 3

0

你最好使用 SeekBar

<SeekBar
     android:id="@+id/sb"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:max="10"
     android:thumb="@drawable/ic_location"
     android:theme="@style/Widget.AppCompat.SeekBar.Discrete" />
于 2020-05-19T16:41:45.503 回答
0

您可以使用 aSeekBar并定义值的范围,并且为了消除平滑度/细化滚动,设置OnSeekBarChangeListener它并通过再次设置进度来覆盖您需要onProgressChanged()的值的有效范围,如下所示。SeekBar

例子

使用SeekBar最大值为 100,步长为 10 的 a;所以总有效值为 10。

SeekBar seekBar = findViewById(R.id.seekbar);
seekBar.setMax(100);

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean
            fromUser) {

        seekBar.setProgress((progress / 10) * 10);

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
});
于 2020-08-14T02:54:14.020 回答
0

对我有用的解决方案是这个名为BubbleSeekBar的库

第 1 步 - 在 Gradle 上添加依赖项。

implementation "com.xw.repo:bubbleseekbar:3.20-lite"

第 2 步 - 在 XML 上创建 BubbleSeekBar 并添加所需的属性。就我而言,下面的示例有效。

<com.xw.repo.BubbleSeekBar
        android:id="@+id/bubbleSeekBar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:bsb_auto_adjust_section_mark="true"
        app:bsb_hide_bubble="true"
        app:bsb_min="2"
        app:bsb_section_count="5"
        app:bsb_section_text_position="below_section_mark"
        app:bsb_seek_by_section="true" />

第 3 步 - 因为我需要自定义选项,所以我使用在我的 string.xml 上声明的值数组在我的 onCreate 上进行了初始化

bubbleSeekBar.setCustomSectionTextArray { sectionCount, array ->
            array.clear()
            for ((index, value) in resources.getStringArray(R.array.xxxx)
                .withIndex()) {
                array.put(index, value)
            }
            array
        }

第 4 步 - 您可以使用以下方法捕获更改或设置值。

bubbleSeekBar.onProgressChangedListener
bubbleSeekBar.setProgress()

该库非常好,对我有用。有关更多信息,请查看此答案顶部的链接。

于 2021-07-15T23:14:32.133 回答