我有一个搜索栏,并尝试在步骤 1 中将第一个搜索栏的值设置为 60 到 180,将第二个搜索栏的值设置为 40 到 190。
sb1 = (SeekBar) findViewById(R.id.progresss);
sb1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
//int inVal = Integer.parseInt(String.valueOf(seekBar.getProgress()));
//inVal =+ 70;
//Toast.makeText(getApplicationContext(), String.valueOf(inVal),Toast.LENGTH_LONG).show();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
progress =+ 70;
Toast.makeText(getApplicationContext(), String.valueOf(progress),Toast.LENGTH_LONG).show();
}
});
不工作。知道如何解决吗?
您无法设置 SeekBar 的
min
值(始终为 0),也无法设置 SeekBar 的 step
值(始终为 1)。
要将值从60设置为180,步长为1:
int step = 1;
int max = 180;
int min = 60;
// Ex :
// If you want values from 3 to 5 with a step of 0.1 (3, 3.1, 3.2, ..., 5)
// this means that you have 21 possible values in the seekbar.
// So the range of the seek bar will be [0 ; (5-3)/0.1 = 20].
seekbar.setMax( (max - min) / step );
seekbar.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)
{
// Ex :
// And finally when you want to retrieve the value in the range you
// wanted in the first place -> [3-5]
//
// if progress = 13 -> value = 3 + (13 * 0.1) = 4.3
double value = min + (progress * step);
}
}
);
我在代码中放置了另一个示例,以便您理解数学。
您可以使用以下代码设置搜索栏的最大值:
sb1.setMax(100);
这将为您的搜索栏设置最大值。
但是你不能设置最小值,但是你可以做一些算术来调整值。 使用算术来调整您的应用程序所需的值。
例如,假设您想要从 -50 到 100 的数据值 显示在SeekBar上。将SeekBar的最大值设置为150(100-(-50)), 然后从原始值中减去 50 即可得到设置时应使用的数字 酒吧位置。
您可以通过此链接获取更多信息。
seekbar.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) {
int MIN = 5;
if (progress < MIN) {
value.setText(" Time Interval (" + seektime + " sec)");
} else {
seektime = progress;
}
value.setText(" Time Interval (" + seektime + " sec)");
}
});
为我设置搜索栏的最小值和最大值的最简单方法:如果您想要值 min=60 到 max=180,则这等于 min=0 max=120。所以在你的seekbar xml中设置属性:
android:max="120"
min 将始终为 0。
现在您只需要做您正在做的事情,添加金额即可在任何更改中获得翻译值,在本例中为+60。
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int translatedProgress = progress + 60;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
要小心seekbar属性android:progress,如果你改变范围,你必须重新计算你的初始进度。如果你想要 50%,max/2,在我的例子中 120/2 = 60;
Seek Bar 有设置最大值的方法,但没有设置最小值的方法 在这里我编写了一个用于设置最小搜索栏值的代码 当我们添加此代码时,您的搜索栏值不小于 mim 值 试试这个,对我来说效果很好
/* This methods call after seek bar value change */
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
/* Check the current seekbar value is greather than min value*/
if (progress < MIN_VALUE) {
/* if seek bar value is lesser than min value then set min value to seek bar */
seekBar.setProgress(MIN_VALUE);
}
}
如果您使用的是 AndroidX 库 (
import androidx.preference.*
),则此功能无需任何 hacky 解决方法即可存在!
val seekbar = findPreference("your_seekbar") as SeekBarPreference
seekbar.min = 1
seekbar.max = 10
seekbar.seekBarIncrement = 1
对于这样的要求,我创建了实用程序来自定义 Seekbar 进度,如下代码:
SeekBarUtil.class
import android.widget.SeekBar;
import android.widget.TextView;
public class SeekBarUtil {
public static void setSeekBar(SeekBar mSeekbar, int minVal, int maxVal, int intervalVal, final TextView mTextView, String startPrefix, String endSuffix) {
int totalCount = (maxVal - minVal) / intervalVal;
mSeekbar.setMax(totalCount);
mSeekbar.setOnSeekBarChangeListener(new CustomSeekBarListener(minVal, maxVal, intervalVal) {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//progress = ((int)Math.round(progress/interval))*interval;
int val = min;
if (interval == totalCount) {
val = max;
} else {
val = min + (progress * interval);
}
seekBar.setProgress(progress);
mTextView.setText(startPrefix + val + endSuffix);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onStopTrackingTouch(SeekBar seekBar) { }
});
}
}
和 CustomSeekBarListener.class
import android.widget.SeekBar;
class CustomSeekBarListener implements SeekBar.OnSeekBarChangeListener {
int min=0,max=0,interval=1;
int totalCount;
public CustomSeekBarListener(int min, int max, int interval) {
this.min = min;
this.max = max;
this.interval = interval;
totalCount= (max - min) / interval;
}
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) { }
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onStopTrackingTouch(SeekBar seekBar) { }
}
您可以像下面的代码片段一样使用它
SeekBarUtil.setSeekBar(seekbarAmountNeeded,10000,200000,5000,textAmount,"$"," PA");
处理这种情况的另一个解决方案是创建一个自定义的 Seekbar,以便每次都可以转换实际值和 SeekBar 进度:
import android.content.Context
import android.util.AttributeSet
import android.widget.SeekBar
//
// Require SeekBar with range [Min, Max] and INCREMENT value,
// However, Android Seekbar starts from 0 and increment is 1 by default, Android supports min attr on API 26,
// To make a increment & range Seekbar, we can do the following conversion:
//
// seekbar.setMax((Max - Min) / Increment)
// seekbar.setProgress((actualValue - Min) / Increment)
// seekbar.getProgress = Min + (progress * Increment)
//
// The RangeSeekBar is responsible for handling all these logic inside the class.
data class Range(val min: Int, val max: Int, private val defaultIncrement: Int) {
val increment = if ((max - min) < defaultIncrement) 1 else defaultIncrement
}
internal fun Range.toSeekbarMaximum(): Int = (max - min) / increment
class RangeSeekBar: SeekBar, SeekBar.OnSeekBarChangeListener {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
var range: Range = Range(0, 100, 1)
set(value) {
field = value
max = value.toSeekbarMaximum()
}
var value: Int = 0
get() = range.min + progress * range.increment
set(value) {
progress = (value - range.min) / range.increment
field = value
}
var onSeekBarChangeListenerDelegate: OnSeekBarChangeListener? = this
override fun setOnSeekBarChangeListener(l: OnSeekBarChangeListener?) {
onSeekBarChangeListenerDelegate = l
super.setOnSeekBarChangeListener(this)
}
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
onSeekBarChangeListenerDelegate?.onProgressChanged(seekBar, value, fromUser)
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
onSeekBarChangeListenerDelegate?.onStartTrackingTouch(seekBar)
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
onSeekBarChangeListenerDelegate?.onStopTrackingTouch(seekBar)
}
}
然后在你的片段中,
// init
range_seekbar.range = Range(10, 110, 10)
range_seekbar.value = 20
// observe value changes
range_seekbar.userChanges().skipInitialValue().subscribe {
println("current value=$it")
}
关键字:Kotlin、范围 SeekBar、Rx
您可以使用材料设计滑块代替搜索栏
<com.google.android.material.slider.Slider
...
android:valueFrom="60"
android:valueTo="180"
android:stepSize="10.0" />'
设置搜索栏最大值和最小值
seekbar have method that setmax(int position) and setProgress(int position)
谢谢
最小值始终从零开始,您对此无能为力。但是当用户开始滚动它时,您可以更改它的值。
这里我将最大值设置为 64。 这个计算很简单:我希望用户选择 15 分钟到 16 小时之间的一个时间,他每 15 分钟到 16 小时选择一个时间,清楚吗?我知道,很简单:)
SeekBar seekBar = (SeekBar) dialog.findViewById(R.id.seekBar);
seekBar.setMax(64);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
float b;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float des = (float) progress / 4;
b = des;
hours.setText(des + " hours");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
hoursSelected = b;
}
});
private static final int MIN_METERS = 100;
private static final int JUMP_BY = 50;
metersText.setText(meters+"");
metersBar.setProgress((meters-MIN_METERS));
metersBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
progress = progress + MIN_METERS;
progress = progress / JUMP_BY;
progress = progress * JUMP_BY;
metersText.setText((progress)+"");
}
});
}
paySeekRange.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Debug.i(TAG, "onProgressChanged 1: " + progress);
int progressMin = (progress * (maxPayRange - minPayRange) / 100) + minPayRange;
Debug.i(TAG, "onProgressChanged 2: " + progress);
int progressMax = (progress * (maxPayRange) / 100);
progress = (progress * (progressMax - progressMin) / 100) + progressMin;
Debug.i(TAG, "onProgressChanged 3: " + progress);
txtWeeklyPay.setText("$".concat(String.valueOf(progress)));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
复制此类并使用自定义搜索栏:
public class MinMaxSeekBar extends SeekBar implements SeekBar.OnSeekBarChangeListener {
private OnMinMaxSeekBarChangeListener onMinMaxSeekBarChangeListener = null;
private int intMaxValue = 100;
private int intPrgress = 0;
private int minPrgress = 0;
public int getIntMaxValue() {
return intMaxValue;
}
public void setIntMaxValue(int intMaxValue) {
this.intMaxValue = intMaxValue;
int middle = getMiddle(intMaxValue, minPrgress);
super.setMax(middle);
}
public int getIntPrgress() {
return intPrgress;
}
public void setIntPrgress(int intPrgress) {
this.intPrgress = intPrgress;
}
public int getMinPrgress() {
return minPrgress;
}
public void setMinPrgress(int minPrgress) {
this.minPrgress = minPrgress;
int middle = getMiddle(intMaxValue, minPrgress);
super.setMax(middle);
}
private int getMiddle(int floatMaxValue, int minPrgress) {
int v = floatMaxValue - minPrgress;
return v;
}
public MinMaxSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOnSeekBarChangeListener(this);
}
public MinMaxSeekBar(Context context) {
super(context);
this.setOnSeekBarChangeListener(this);
}
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
intPrgress = minPrgress + i;
onMinMaxSeekBarChangeListener.onMinMaxSeekProgressChanged(seekBar, intPrgress, b);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
onMinMaxSeekBarChangeListener.onStartTrackingTouch(seekBar);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
onMinMaxSeekBarChangeListener.onStopTrackingTouch(seekBar);
}
public static interface OnMinMaxSeekBarChangeListener {
public void onMinMaxSeekProgressChanged(SeekBar seekBar, int i, boolean b);
public void onStartTrackingTouch(SeekBar seekBar);
public void onStopTrackingTouch(SeekBar seekBar);
}
public void setOnIntegerSeekBarChangeListener(OnMinMaxSeekBarChangeListener floatListener) {
this.onMinMaxSeekBarChangeListener = floatListener;
}
}
此类包含方法
,用于设置Seek Bar的最小值 该类包含方法public void setMin(int minPrgress)
用于设置 Seek Bar 的最大值public void setMax(int maxPrgress)
现在你可以使用app:min,就像这个app:min="@integer/min_circle_tracker_timeout"
seekbar 中没有设置最小值或最大值的选项,因此您可以在此处使用公式来缩放您的值。
Desired_value = ( progress * ( Max_value - Min_value) / 100 ) + Min_value
我已经在很多例子中测试了这个公式。在你的例子中, 如果进度条位于中间(即进度 = 50 )并且您的 Min_val 和 Max_val 分别为 60 和 180,那么此公式将为您提供 Desired_value '120'。