Android seekbar如何阻止在进度值范围内拖动

Android seekbar how to block dragging in range of progress value

为了清楚起见,我想防止在最大值的 80% 处拖动,同时留出 20% 的边距。拖动范围将为(从 20% 到 80%)。

我只需要知道是否有可能使现有方法变得清晰和简单。

截图

关于

的回答here中提到

You can't define the minimum value. It is 0.

如果你想设置最大值你可以做到

来自 xml

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="your_max_value"/>

使用java

seekBar = (SeekBar)findViewById(R.id.seekbar_id);
seekBar.setMax(your_max_value);

您可以使用 onProgressChanged 进行非常基本的计算。如果最小值为 0,最大值为 200,我们想限制前 20% 和后 20%。在 onProgressChanged 方法中,您只需:

// I think this two variables should be out of the method
int limit = ((20 * 200) / 100);
int maxValue = seekBar.getMax();
if(seekBar.getProgress() >= (maxValue - limit)){
    seekBar.setProgress(maxValue);
}else if(seekBar.getProgress() <= limit){
    seekBar.setProgress(limit);
}

这是即时完成的,没有经过测试,可能有问题,但就是这个想法