2013-09-01 35 views
3

我擴展了NumberPicker類,我將它重寫爲setOnValueChangedListener,但是當我調試代碼時,我點擊了numberpicker的加號按鈕,它永遠不會傳入方法setOnValueChangedListener中的斷點處。 有什麼不對嗎?Android的numberpicker,永遠不會從OnValueChangedListener斷點傳遞

@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(); 
        } 
       } 
      }; 
     } 
    } 
} 
+0

您的自定義NumberPicker你不需要自定義數字選擇器設定分鐘最大值。你可以使用'np.setMaxValue(100); np.setMinValue(0)'。爲什麼你需要一個自定義數字選擇器? – Raghunandan

+1

是的,我知道,我只認爲它更優雅:D 但問題不是這個,你知道爲什麼聽者是錯的嗎? – Accollativo

回答

1

您可以initializze您的自定義數字選擇器

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

快照:

當您更改值時,您會看到烤麪包根據您的要求進行相同的修改。

enter image description here

編輯:

更改如下

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

快照

enter image description here

+0

謝謝Rag!我已經知道這種方法來解決它。我只是想知道爲什麼我不能設置自定義類的偵聽器。也許我錯了,但我認爲它適合從自定義類中設置它。 我只是在這裏學習一些東西:D謝謝! ;) – Accollativo

+0

@Accollat​​ivo你必須初始化你的自定義數字選擇器,然後設置相同的偵聽器。就像按鈕。您初始化並設置偵聽器。 – Raghunandan

+0

@Accollat​​ivo是的,你可以。檢查編輯 – Raghunandan

相關問題