Android SeekBar最小值

Sac*_*hin 76 java android seekbar android-view

有谁知道如何定义SeekBar的最小值?这是在XML布局中完成还是我需要以编程方式定义它?

基本上我需要将最小值从0更改为0.2

Com*_*are 117

嗨,有谁知道如何定义SeekBar的最小值?

您无法定义最小值.是的0.

基本上我需要将最小值从0更改为0.2

获得该值后,添加0.2到该值.

  • @ user1617737:不,你不应该.他的回答与最低价值无关.`SeekBar`表示从0到(最大 - 最小)的范围.要获得调整后的值,请将所需的最小值添加到实际进度中.由于实际进度已经"反映在UI上",因此没有什么可以改变的. (4认同)

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);

  • 是的,我认为这是最好的方法。SeekBar的单位仅仅是抽象值。您可以简单地在代码中对其进行调整,以获得所需的实际单位,因此我对您的回答进行了投票。 (2认同)

SAL*_*MAN 9

基本上我添加了大小+ 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)

  • 请不要使用注释来解释答案中的代码,请将其作为答案本身的一部分. (10认同)

ban*_*ing 9

对于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上提供正确的最小值,最大值和比例,如果用户将其一直向左滑动,控件的位置将不会"向右"跳跃.


Joã*_*ães 6

api 版本 >=26 中,您现在可以min向 SeekBar添加xml 属性:

android:min="0.2"


Sza*_*cze 5

我的解决方案对我来说很好:

    @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)