SeekBar setMin在Android上至少需要api 26?

问题描述 投票:3回答:2

我想在我的Android应用程序中使用SeekBar。我的minsdk版本必须为23。编译器说SeekBar的setMin至少需要API级别26。我需要一些特殊的支持库来实现简单的SeekBar setMin吗?

我在Linux上使用Android Studio 3.0.1。我的build.gradle是这样的:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.zamek.boyler"
        minSdkVersion 23
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    ...


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

我的布局代码段:

<SeekBar
        android:id="@+id/sb_hysteresis"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="15dp"/>

我的活动代码段:

import android.widget.SeekBar;
...
 private SeekBar hysteresis;
...
this.hysteresis = findViewById(R.id.sb_hysteresis);
this.hysteresis.setMin(10); <--Compiler said:Call requires API level 26 (current min is 23): android.widget.AbsSeekBar#setMin

thx,扎梅克

android android-seekbar
2个回答
6
投票

SeekBar setMin()方法已在API级别26中添加。

如果要限制SeekBar最小值,则必须手动实施。

 seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int min = 5;
                if(progress < min) {
                    seekBar.setProgress(min);
                }

            }

2
投票

我同意Zeeshan's response,但我的处理方法有所不同,也许会帮助尝试实现这一目标的人。

首先定义您的最小值和最大值。

private static int MAX_VALUE = 220;
private static int MIN_VALUE = 50;

然后像这样设置seekbar max。这样,您将使搜索栏仅包含您要定义的时间间隔的值。

seekbar.setMax(MAX_VALUE - MIN_VALUE);

此后,每当您检查搜寻栏的值时,都必须先添加我们定义的最小值。

@Override
public void onProgressChanged(SeekBar seekBar, int value, boolean fromUser) {
    displayValue((value + MIN_VALUE) + "cm");
}
© www.soinside.com 2019 - 2024. All rights reserved.