2014-12-07 138 views
4

當numberpicker被加載時,默認值沒有出現在屏幕上直到被觸摸。Android Numberpicker的默認值沒有出現

一旦感動,一切正常。任何幫助表示讚賞。

此外,如果格式化程序被刪除,它工作正常。

enter image description here

dialog.xml

<NumberPicker 
    android:id="@+id/number_picker" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    /> 

<Button 
    android:id="@+id/apply_button" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/number_picker" 
    android:text="@string/ok_string" /> 

這裏是活動的代碼:

final NumberPicker np = (NumberPicker) d.findViewById(R.id.number_picker); 
     np.setMaxValue(50); 
     np.setMinValue(0); 
     np.setWrapSelectorWheel(true); 
     np.setOnValueChangedListener(this); 
     np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 

     np.setFormatter(new NumberPicker.Formatter() { 
      @Override 
      public String format(int i) { 
       if (i == 25) 
        return "0"; 
       else 
        return String.valueOf(i - 25); 
      } 
     }); 
     np.setValue(25); 

在此先感謝

+0

難道是初始文本顏色與背景顏色相匹配嗎?很難想象沒有設備時你所看到的內容。 – stkent 2014-12-07 14:51:33

+0

[格式化的Android NumberPicker可能重複不會在第一次呈現時格式化](https://stackoverflow.com/questions/17708325/android-numberpicker-with-formatter-does-not-format-on-first-rendering) – 2017-07-14 09:31:41

回答

4

該問題似乎是NumberPicker窗口小部件中的一個錯誤。

我已經使用方法2解決了它here

0

我發現在NumberPicker在18-26的API不使用反射,並且不使用setDisplayedValues()here的工作原理是錯誤的解決方案。

0

我有同樣的問題,我正在使用NumberPicker與字符串。 我的問題是,將活性物用過渡打開後的數選取器的默認值是不可見的,即使我用picker.setDisplayedValues(list.toStringsArray())

enter image description here

所以我的解決方案是設置選擇器值以下內容:

private void populatePicker(NumberPicker picker, String[] strings, int index) { 
    picker.setDisplayedValues(null); 
    picker.setMinValue(0); 
    picker.setMaxValue(strings.length - 1); 
    picker.setDisplayedValues(strings); 
    picker.setValue(index); 

    try { 
     Field field = NumberPicker.class.getDeclaredField("mInputText"); 
     field.setAccessible(true); 
     EditText inputText = (EditText) field.get(picker); 
     inputText.setVisibility(View.INVISIBLE); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
}