Java Android SeekBar 最小值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3033135/
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
Android SeekBar Minimum Value
提问by Sachin
How to define a SeekBar's minimum value? Is this done in the XML layout or do I need to define it programatically?
如何定义 SeekBar 的最小值?这是在 XML 布局中完成的还是我需要以编程方式定义它?
Basically I need to change my minimum value from 0 to 0.2
基本上我需要将最小值从 0 更改为 0.2
采纳答案by CommonsWare
How to define a SeekBar's minimum value?
如何定义 SeekBar 的最小值?
You can't define the minimum value. It is 0
.
您无法定义最小值。它是0
。
Basically I need to change my minimum value from 0 to 0.2
基本上我需要将最小值从 0 更改为 0.2
When you get the value, add 0.2
to it.
当您获得价值时,添加0.2
它。
回答by SALMAN
Basically I have added the size+10 which will automatically set the min to 10 you can change yours to set it which min. value.
基本上我已经添加了 size+10,它会自动将 min 设置为 10,您可以更改您的设置以将其设置为 min。价值。
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int siz = size + 10;
txtViewFontSize.setTextSize(siz);
Toast.makeText(this, String.valueOf(siz), Toast.LENGTH_SHORT).show();
}
回答by Joao Sousa
I created this simple library, it's not much, but it can save you some time.
我创建了这个简单的库,虽然不多,但可以为您节省一些时间。
回答by Szabolcs Becze
My solution which works fine for me is:
我的解决方案对我来说很好用:
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress <= SOME_LIMIT) {
seekBar.setProgress(SOME_LIMIT);
} else {
// DO SOMETHING
}
}
回答by Asthme
Here is what I use to get android:max
for a max/min range for a SeekBar
.
这是我用来获得android:max
最大/最小范围的SeekBar
.
mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressChanged = minimumValue;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progressChanged = minimumValue+ progress;
}
});
So you will get the range minimum to minimum+max
;
所以你会得到范围minimum to minimum+max
;
To set the max value in xml, use max=(desiredMax-min)
;
要在 xml 中设置最大值,请使用max=(desiredMax-min)
;
回答by ban-geoengineering
The min value must be 0 for the SeekBar object in Java (i.e., it cannot be changed), but this is how to get the required look and performance.
Java 中的 SeekBar 对象的最小值必须为 0(即不能更改),但这是获得所需外观和性能的方法。
Suppose you want your min to be 5 and your max to be 100...
假设您希望最小值为 5,最大值为 100...
Therefore, you have a range of 95, so you would need to set up the SeekBar with a maximum value of 95
.
因此,您的范围为 95,因此您需要将 SeekBar 设置为最大值95
。
If your seekbar has UI labels, you would make them 5
(min) and 100
(max).
如果您的搜索栏有 UI 标签,您可以将它们设为5
(min) 和100
(max)。
So, the layout XML would be something like...
因此,布局 XML 将类似于...
<LinearLayout
android:id="@+id/sampleSizeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/batchDurationSecsLayout"
android:orientation="vertical"
android:visibility="visible" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sample_size" />
<SeekBar
android:id="@+id/sampleSizeSeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="95" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="visible" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:text="5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="right"
android:text="100" />
</RelativeLayout>
</LinearLayout>
So nowyou can introduce the 5
correction, like this...
所以现在你可以引入5
更正了,就像这样......
final SeekBar sampleSizeSeekBar = (SeekBar)findViewById(R.id.sampleSizeSeekBar);
final int sampleSizeSeekBarCorrection = 5; //e.g., 95 <--> 100
int realValueFromPersistentStorage = appObj.getSampleSize(); //Get initial value from persistent storage, e.g., 100
sampleSizeSeekBar.setProgress(realValueFromPersistentStorage - sampleSizeSeekBarCorrection); //E.g., to convert real value of 100 to SeekBar value of 95.
sampleSizeSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int val = sampleSizeSeekBar.getProgress();
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
val = progress + sampleSizeSeekBarCorrection; //e.g., to convert SeekBar value of 95 to real value of 100
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
try {
appObj.saveSampleSize(val); //Save real value to persistent storage, e.g., 100
appObj.makeToast("Sample size set to " + val);
}
catch(Exception e) {
Log.e(LOG_TAG, "Error saving sample size", e);
appObj.makeToast("Error saving sample size: " + e);
}
}
});
This solution will give your seekbar the correct min, max and scale on the UI, and the position of the control won't 'jump' to the right if the user slides it all the way to the left.
此解决方案将在 UI 上为您的搜索栏提供正确的最小值、最大值和比例,并且如果用户将其一直向左滑动,则控件的位置不会向右“跳”。
回答by Dharmesh
You can not set progress in float because seekbar.getProgress() always returns integer value. Following code will set textview value to 0.2 if seekbar value is 0. you can change minimumVal according to your requirement.
您不能在 float 中设置进度,因为 seekbar.getProgress() 始终返回整数值。如果 seekbar 值为 0,以下代码会将 textview 值设置为 0.2。您可以根据需要更改 minimumVal。
int minimumVal = 0;
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress >= minimumVal) {
seekBar.setProgress(progress);
textView.setText(progress);
} else {
seekBar.setProgress(minimumVal);
textView.setText("0.2");
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
回答by Sasha
You can try doing this way:
您可以尝试这样做:
int yourMinProgress = 10;
int yourMinProgress = 10;
private SeekBar.OnSeekBarChangeListener onSeekBarChangeListener =
new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
if (progress < yourMinProgress) {
seekBar.setProgress(10);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
回答by Karthick
this code works for me fine!!!
这段代码对我有用!!!
SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar);
seekBar.setMax(100);
seekBar.setProgress(40);
seekBar.setOnSeekBarChangeListener(seekBarChangeListener);
SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
if(progress <= 40){
seekBar.setProgress(40);
}
if(progress >= 40) {
pgr_status.setText(String.valueOf(progress));
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
回答by Jo?o Magalh?es
In api versions >=26you can now add min
xml attribute to SeekBar:
在api 版本 >=26 中,您现在可以min
向 SeekBar添加xml 属性:
android:min="0.2"
android:min="0.2"