要启用数字选择器,请使用以下代码:
final NumberPicker numberPicker = (NumberPicker) findViewById(R.id.numberPicker1);
numberPicker.setMaxValue(100);
numberPicker.setMinValue(1);
numberPicker.setWrapSelectorWheel(true);
但问题是,我希望数字选择器只在我滚动它时才能看到屏幕截图,当它是静态的时(重新计算未被触摸)我希望它只显示已选择的数字,而不是那个在当前号码之前和之后。我怎样才能做到这一点?
答案 0 :(得分:0)
创建一个defalult可见的TextView或按钮,以及默认情况下不可见的NumberPicker,但它位于布局XML中的文本视图之上。然后,将setOnClickListener或setOnFocusChangeListener添加到将更改NumberPicker可见性的文本视图中。
您需要定义何时再次隐藏NumberPicker的逻辑。我建议在失去焦点时隐藏它,或者在一段时间(几秒钟)后它的值不会改变。
答案 1 :(得分:-1)
我不确定这是否有效,但您可以像{em>}那样扩展NumberPicker
类:
public class MyNumberPicker extends NumberPicker {
private float bottomFadingEdgeStrength = 1.0f;
private float topFadingEdgeStrength = 1.0f;
public MyNumberPicker(Context context) {
super(context);
}
public MyNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyNumberPicker(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected float getBottomFadingEdgeStrength() {
return bottomFadingEdgeStrength;
}
@Override
protected float getTopFadingEdgeStrength() {
return topFadingEdgeStrength;
}
void setTopFadingEdgeStrength(float strength){
topFadingEdgeStrength = strength;
}
void setBottomFadingEdgeStrength(float strength){
bottomFadingEdgeStrength = strength;
}
}
然后,通过监听滚动事件,根据滚动状态设置渐弱边缘强度:
final MyNumberPicker np = new MyNumberPicker(this);
np.setOnScrollListener(new NumberPicker.OnScrollListener() {
@Override
public void onScrollStateChange(NumberPicker view, int scrollState) {
if (scrollState == NumberPicker.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){
np.setBottomFadingEdgeStrength(0.9f);
np.setBottomFadingEdgeStrength(0.9f);
} else if (scrollState == NumberPicker.OnScrollListener.SCROLL_STATE_IDLE){
np.setBottomFadingEdgeStrength(1.0f);
np.setBottomFadingEdgeStrength(1.0f);
}
//also you can handle SCROLL_STATE_FLING if you want
}
});