3

I extended the NumberPicker class, I overrided to the setOnValueChangedListener but when I debug the code and I click on the plus button of the numberpicker, it never pass in my breakpoint inside the method setOnValueChangedListener. Something is wrong?

@TargetApi(Build.VERSION_CODES.HONEYCOMB)//For backward-compability
public class NumberPickerCustom extends NumberPicker 
{
    private int clickNumber = 0;

    public NumberPickerCustom(Context context) {
        super(context);
    }

    public NumberPickerCustom(Context context, AttributeSet attrs) {
        super(context, attrs);
        processAttributeSet(attrs);
    }

    public NumberPickerCustom(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        processAttributeSet(attrs);
    }
    private void processAttributeSet(AttributeSet attrs) {
        //This method reads the parameters given in the xml file and sets the properties according to it
        this.setMinValue(attrs.getAttributeIntValue(null, "min", 0));
        this.setMaxValue(attrs.getAttributeIntValue(null, "max", 0));
        this.setValue(attrs.getAttributeIntValue(null, "value", 0));
    }

    @Override
    public void setOnValueChangedListener(
            OnValueChangeListener onValueChangedListener) 
    {
        super.setOnValueChangedListener(onValueChangedListener);
        if (onValueChangedListener==null)
        {
            onValueChangedListener = new OnValueChangeListener() {

                @Override
                public void onValueChange(NumberPicker picker, int oldVal, int newVal) 
                {
                    clickNumber++;
                    if (clickNumber>10)
                    {
                        clickNumber = -1000;
                        Toast.makeText(getContext(), "Clicca sul numero centrale per inserire direttamente un valore", Toast.LENGTH_SHORT).show();
                    }
                }
            };
        }
    }
}
4

1 回答 1

1

您可以初始化您的自定义号码选择器

 View rootView = inflater.inflate(R.layout.fragment_random_block, container, false);
 NumberPickerCustom np = (NumberPickerCustom) rootView.findViewById(R.id.numberPickerCustom);
 np.setOnValueChangedListener(this);

确保你的类实现

  implements OnValueChangeListener

然后

@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
    Toast.makeText(MainActivity.this, "Clicca sul numero centrale per inserire direttamente un valore", Toast.LENGTH_SHORT).show();
     }
}

快照:

当您更改值时,您会看到吐司根据您的要求进行了相同的修改。

在此处输入图像描述

编辑 :

如下更改您的自定义 NumberPicker

public class NumberPickerCustom extends NumberPicker implements OnValueChangeListener
{
    public NumberPickerCustom(Context context) {
        super(context);
    }

    public NumberPickerCustom(Context context, AttributeSet attrs) {
        super(context, attrs);
        processAttributeSet(attrs);
    }

    public NumberPickerCustom(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        processAttributeSet(attrs);
    }
    private void processAttributeSet(AttributeSet attrs) {
        //This method reads the parameters given in the xml file and sets the properties according to it
        this.setMinValue(attrs.getAttributeIntValue(null, "min", 0));
        this.setMaxValue(attrs.getAttributeIntValue(null, "max", 0));
        this.setValue(attrs.getAttributeIntValue(null, "value", 0));
        this.setOnValueChangedListener(this);
    }

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        Toast.makeText(getContext(), "New Value is"+newVal, Toast.LENGTH_SHORT).show();
    }  
}

快照

在此处输入图像描述

于 2013-09-02T05:11:28.447 回答