Sac*_*hin 76 java android seekbar android-view
有谁知道如何定义SeekBar的最小值?这是在XML布局中完成还是我需要以编程方式定义它?
基本上我需要将最小值从0更改为0.2
Com*_*are 117
嗨,有谁知道如何定义SeekBar的最小值?
您无法定义最小值.是的0
.
基本上我需要将最小值从0更改为0.2
获得该值后,添加0.2
到该值.
Ast*_*hme 21
这是我用来获得android:max
最大/最小范围的SeekBar
.
mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressChanged = minimumValue;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progressChanged = minimumValue+ progress;
}
});
Run Code Online (Sandbox Code Playgroud)
所以你会得到范围minimum to minimum+max
;
要在xml中设置最大值,请使用max=(desiredMax-min)
;
基本上我添加了大小+ 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();
}
Run Code Online (Sandbox Code Playgroud)
对于Java中的SeekBar对象,min值必须为0(即,它不能更改),但这是获得所需外观和性能的方法.
假设你希望你的分数为5而你的最大值为100 ......
因此,您的范围为95,因此您需要设置SeekBar,其最大值为95
.
如果您的搜索栏有UI标签,您可以制作它们5
(分钟)和100
(最大).
所以,布局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>
Run Code Online (Sandbox Code Playgroud)
所以现在你可以介绍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);
}
}
});
Run Code Online (Sandbox Code Playgroud)
此解决方案将为您的搜索栏在UI上提供正确的最小值,最大值和比例,如果用户将其一直向左滑动,控件的位置将不会"向右"跳跃.
我的解决方案对我来说很好:
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress <= SOME_LIMIT) {
seekBar.setProgress(SOME_LIMIT);
} else {
// DO SOMETHING
}
}
Run Code Online (Sandbox Code Playgroud)