Java 如何设置搜索栏的最小值和最大值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20762001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set seekbar min and max value
提问by Si8
I have a seekbar and trying to set the value from 60 to 180 for one and 40 to 190 for the second one in step of 1.
我有一个搜索栏,并试图在第 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();
}
});
is not working. Any idea how to fix it?
不管用。知道如何修复它吗?
采纳答案by ishu
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)");
}
});
回答by Md Abdul Gafur
Set seekbar max and min value
设置搜索栏的最大值和最小值
seekbar have method that setmax(int position) and setProgress(int position)
thanks
谢谢
回答by Kailash Dabhi
You can set max value for your seekbar by using this code:
您可以使用以下代码为您的搜索栏设置最大值:
sb1.setMax(100);
This will set the max value for your seekbar.
这将为您的搜索栏设置最大值。
But you cannot set the minimum value but yes you can do some arithmetic to adjust value. Use arithmetic to adjust your application-required value.
但是你不能设置最小值,但是你可以做一些算术来调整值。使用算术来调整您的应用程序所需的值。
For example, suppose you have data values from -50 to 100 you want to display on the SeekBar. Set the SeekBar's maximum to be 150 (100-(-50)), then add 50 to the raw value to get the number you should use when setting the bar position.
例如,假设您希望在 SeekBar 上显示从 -50 到 100 的数据值。将 SeekBar 的最大值设置为 150 (100-(-50)),然后将 50 添加到原始值以获得设置条位置时应使用的数字。
You can get more info via this link.
您可以通过此链接获取更多信息。
回答by user3131373
Seek Bar has methods for setting max values but not for setting min value here i write a code for setting minimum seek bar value when we add this code then your seek bar values not less then mim value try this its work fine for me
搜索栏有设置最大值的方法,但不是在这里设置最小值的方法我写了一个代码来设置最小搜索栏值,当我们添加这个代码然后你的搜索栏值不小于 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);
}
}
回答by WannaGetHigh
You cannot set the min
value of a SeekBar (always 0) and you cannot set the step
value of a SeekBar (always 1).
您不能设置min
SeekBar的值(始终为 0),也不能设置step
SeekBar的值(始终为 1)。
To set the value from 60to 180with a step of 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);
}
}
);
I put another example within the code so that you understand the math.
我在代码中放了另一个例子,以便你理解数学。
回答by Muhammed Thasneem
Copy this class and use custom Seek Bar :
复制此类并使用自定义搜索栏:
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;
}
}
This class contin method
public void setMin(int minPrgress)
for setting minimum value of Seek Bar This class contin methodpublic void setMax(int maxPrgress)
for setting maximum value of Seek Bar
此类
public void setMin(int minPrgress)
设置Seek Bar最小值的contin方法此类public void setMax(int maxPrgress)
设置Seek Bar最大值的contin方法
回答by Sindri Tór
Min-value will always start at zero and its nothing you can do about it. But you can change its value when user start scrolling it around.
最小值总是从零开始,对此您无能为力。但是当用户开始滚动它时,您可以更改它的值。
Here I set the max-value as 64. This calculations are simple: I want the user to pick a time from 15min to 16 hours, and he picks one of every 15min to 16 hours, clear? I know, very simple :)
这里我设置最大值为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;
}
});
回答by Pelanes
The easiest way to set a min and max value to a seekbar for me: if you want values min=60 to max=180, this is equal to min=0 max=120. So in your seekbar xml set property:
为我的搜索栏设置最小值和最大值的最简单方法:如果您想要值 min=60 到 max=180,这等于 min=0 max=120。所以在你的 seekbar xml 设置属性中:
android:max="120"
min will be always 0.
min 将始终为 0。
Now you only need to do what your are doing, add the amount to get your translated value in any change, in this case +60.
现在您只需要做您正在做的事情,添加金额以在任何更改中获得您的翻译值,在本例中为 +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) {
}
});
Be careful with the seekbar property android:progress, if you change the range you must recalculate your initial progress. If you want 50%, max/2, in my example 120/2 = 60;
小心seekbar 属性android:progress,如果你改变了范围,你必须重新计算你的初始进度。如果你想要 50%, max/2, 在我的例子中 120/2 = 60;
回答by Priyanshu Patel
There is no option to set a min or max value in seekbar , so you can use a formula here to scale your value.
在 seekbar 中没有设置最小值或最大值的选项,因此您可以在此处使用公式来缩放您的值。
Desired_value = ( progress * ( Max_value - Min_value) / 100 ) + Min_value
I have tested this formula in many examples. In your example, if the progressBar is the middle(i.e. progress = 50 ) and your Min_val and Max_val are 60 and 180 respectively, then this formula will give you the Desired_value '120'.
我已经在很多例子中测试了这个公式。在您的示例中,如果 progressBar 位于中间(即 progress = 50 )并且您的 Min_val 和 Max_val 分别为 60 和 180,则此公式将为您提供 Desired_value '120'。
回答by TamirB
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)+"");
}
});
}