我有一个搜索栏并尝试将值设置为60到180表示一个,40到190表示第二个步骤为1。
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();
}
});
无效。知道怎么解决吗?
答案 0 :(得分:94)
您无法设置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);
}
}
);
我在代码中添加了另一个示例,以便您理解数学。
答案 1 :(得分:29)
您可以使用以下代码设置搜索栏的最大值:
sb1.setMax(100);
这将设置搜索栏的最大值。
但是你不能设置最小值但是你可以做一些算术来调整值。 使用算术来调整应用程序所需的值。
例如,假设您希望数据值介于-50到100之间 显示在SeekBar上。将SeekBar的最大值设置为150(100 - ( - 50)), 然后将50添加到原始值以获取设置时应使用的数字 酒吧的位置。
您可以通过此link获取更多信息。
答案 2 :(得分:10)
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)");
}
});
答案 3 :(得分:8)
为我设置最小值和最大值到搜索条的最简单方法:如果您希望值min = 60到max = 180,则等于min = 0 max = 120。所以在你的seekbar xml set属性中:
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;
答案 4 :(得分:4)
Seek Bar有设置最大值的方法,但不能设置最小值 在这里我写了一个代码来设置最小搜索条值 当我们添加此代码时,您的搜索栏值不小于mim值 试试这个对我来说很好的工作
/ *此方法在搜索栏值更改后调用* /
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);
}
}
答案 5 :(得分:1)
处理这种情况的另一种方法是创建自定义的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
答案 6 :(得分: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");
答案 7 :(得分:1)
您可以使用 Material design 滑块代替seekbar
<com.google.android.material.slider.Slider
...
android:valueFrom="60"
android:valueTo="180"
android:stepSize="10.0" />'
答案 8 :(得分:0)
设置搜索栏最大值和最小值
seekbar have method that setmax(int position) and setProgress(int position)
感谢
答案 9 :(得分:0)
复制此课程并使用自定义搜索栏:
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;
}
}
此类Contin方法
public void setMin(int minPrgress)
用于设置Seek Bar的最小值 此类连续方法public void setMax(int maxPrgress)
用于设置Seek Bar的最大值
答案 10 :(得分:0)
最小值总是从零开始,你无能为力。但是,当用户开始滚动它时,您可以更改其值。
这里我将max-value设置为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;
}
});
答案 11 :(得分:0)
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)+"");
}
});
}
答案 12 :(得分:0)
如果您使用的是AndroidX libraries(import androidx.preference.*
),则此功能不存在任何变通方法!
val seekbar = findPreference("your_seekbar") as SeekBarPreference
seekbar.min = 1
seekbar.max = 10
seekbar.seekBarIncrement = 1
答案 13 :(得分:-1)
Log.i(TAG, "onProgressChanged 1: " + progress);
int progressMin = (progress * (maxPayRange - minPayRange) / 100) + minPayRange;
Log.i(TAG, "onProgressChanged 2: " + progress);
int progressMax = (progress * (maxPayRange - minPayRange) / maxPayRange);
progress = (progress * (progressMax - progressMin) / 100) + progressMin;
Log.i(TAG, "onProgressChanged 3: " + progress);
txtWeeklyPay.setText("$" + (progress));
答案 14 :(得分:-2)
没有选项可以在搜索栏中设置最小值或最大值,因此您可以在此处使用公式来缩放您的值。
Desired_value = ( progress * ( Max_value - Min_value) / 100 ) + Min_value
我在很多例子中测试了这个公式。在你的例子中, 如果progressBar是中间的(即progress = 50)而你的Min_val和Max_val分别是60和180,那么这个公式将给你Desired_value&#39; 120&#39;。