2015-12-02 148 views
0

我是Android新手,我在我的應用程序中爲我的某項活動實施了NumberPicker。下面是我的代碼的摘錄:Android NumberPicker更改文本顏色

picker = (NumberPicker)findViewById(R.id.order_confirm_bring_time_minute_picker); 
picker.setMinValue(15); 
picker.setMaxValue(120); 
picker.setWrapSelectorWheel(false); 
setNumberPickerTextColor(picker, android.R.color.black); 

public boolean setNumberPickerTextColor(NumberPicker numberPicker, int color) 
{ 
    final int count = numberPicker.getChildCount(); 
    for(int i = 0; i < count; i++){ 
     View child = numberPicker.getChildAt(i); 
     if(child instanceof EditText){ 
      try{ 
       Field selectorWheelPaintField = numberPicker.getClass() 
         .getDeclaredField("mSelectorWheelPaint"); 
       selectorWheelPaintField.setAccessible(true); 
       ((Paint)selectorWheelPaintField.get(numberPicker)).setColor(color); 
       ((EditText)child).setTextColor(color); 
       numberPicker.invalidate(); 
       return true; 
      } 
      catch(NoSuchFieldException e){ 
       Log.d("setNumberPickerTextColor", "NoSuchFieldException"); 
      } 
      catch(IllegalAccessException e){ 
       Log.d("setNumberPickerTextColor", "IllegalAccessException"); 
      } 
      catch(IllegalArgumentException e){ 
       Log.d("setNumberPickerTextColor", "IllegalArgumentException"); 
      } 
     } 
    } 
    return false; 
} 

我看着this postsetNumberPickerTextColor方法。但它似乎不工作,因爲我將我的顏色設置爲黑色,但它不再可見。如果我不使用setNumberPickerTextColor方法,那麼我的默認顏色是白色,這可以在突出顯示NumberPicker的EditText字段中的文本時看到。

color not changed

這是NumberPicker的屏幕截圖時顏色不改變。

color changed to black

這是NumberPicker的屏幕截圖當顏色變爲黑色或任何其它顏色(我已經測試和它們給予同樣的結果)。

會有一種方法來自定義我的NumberPicker中的文本顏色嗎?此外,我知道這是一個不同的問題,但頂部和底部「酒吧」的顏色以及它們不適合我的應用程序的顏色主題。預先感謝您的幫助。

回答

1

您需要的解決顏色傳給setTextColor方法,而不是資源ID。

((EditText)child).setTextColor(getResources().getColor(color));