问题
I want to use a SeekBar in my android app. My minsdk version is must be 23.
The compiler said setMin of SeekBar needs at least API level 26.
Do I need some special support library for a simple SeekBar setMin?
I use Android Studio 3.0.1 on Linux. My build.gradle is like this:
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'
}
my layout snippet:
<SeekBar
android:id="@+id/sb_hysteresis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"/>
my Activity code snippets:
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,
Zamek
回答1:
SeekBar
setMin()
method was added in API level 26.
If you want to limit your SeekBar
minimum value then you have to implement it manually.
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:
I agree with Zeeshan's response but my approach to it is a bit different, maybe it'll help someone trying to achieve this.
First define your min and max values.
private static int MAX_VALUE = 220;
private static int MIN_VALUE = 50;
Then setup the seekbar max like this. By doing so you will make your seekbar have only the amount of values of the interval you wish to define.
seekbar.setMax(MAX_VALUE - MIN_VALUE);
After that whenever you check for the seekbar's value you must first add the min value that we defined.
@Override
public void onProgressChanged(SeekBar seekBar, int value, boolean fromUser) {
displayValue((value + MIN_VALUE) + "cm");
}
来源:https://stackoverflow.com/questions/48479842/seekbar-setmin-require-at-least-api-26-in-android