2015-12-02 145 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));