我在我的代码中使用了一个搜索栏,带有两个按钮来增加和减少值。搜索栏的最小和最大范围应该是-30到480.
任何人都可以建议如何将搜索栏中的范围设置为-30到480.
答案 0 :(得分:3)
这是我用于此的课程:
import android.content.Context;
import android.support.v7.appcompat.R;
import android.util.AttributeSet;
import android.widget.SeekBar;
/**
* Created by HeWhoWas on 5/10/13.
*/
public class NegativeSeekBar extends SeekBar {
protected int minimumValue = 0;
protected int maximumValue = 0;
protected OnSeekBarChangeListener listener;
public NegativeSeekBar(Context context){
super(context);
setUpInternalListener();
}
public NegativeSeekBar(Context context, AttributeSet attrs){
super(context, attrs);
setUpInternalListener();
}
public NegativeSeekBar(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
setUpInternalListener();
}
public void setMin(int min){
this.minimumValue = min;
super.setMax(maximumValue - minimumValue);
}
public void setMax(int max){
this.maximumValue = max;
super.setMax(maximumValue - minimumValue);
}
@Override
public void setOnSeekBarChangeListener(OnSeekBarChangeListener listener){
this.listener = listener;
}
private void setUpInternalListener(){
super.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(listener != null){
listener.onProgressChanged(seekBar, minimumValue + i, b);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
if(listener != null)
listener.onStartTrackingTouch(seekBar);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if(listener != null)
listener.onStopTrackingTouch(seekBar);
}
});
}
}