2016-11-07 24 views
1

我要實現對MyNumberPicker這種效果如何改變顏色選擇的號碼中啓用了NumberPicker在Android的

enter image description here

到目前爲止,我實現了一個NumberPicker類,並改變一些特性,但最有趣的部分是在這裏:

public class NumberPicker extends android.widget.NumberPicker{ 

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

    @Override 
    public void addView(View child) { 
     super.addView(child); 
     updateView(child); 
    } 

    @Override 
    public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) { 
     super.addView(child, index, params); 
     updateView(child); 
    } 

    @Override 
    public void addView(View child, android.view.ViewGroup.LayoutParams params) { 
     super.addView(child, params); 
     updateView(child); 
    } 

    private void updateView(View view) { 
     if(view instanceof EditText){ 
      ((EditText) view).setTextSize(getResources().getDimension(R.dimen.numberpicker_text_size)); 
      ((EditText) view).setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimaryDark)); 

     } 
    } 

,但我得到的效果是這樣的:

enter image description here

我得到藍色的所有文本(選中,未選中,激活,非激活)如何獲取此藍色只啓用NumberPicker和選定的值?

感謝支持

回答

1

創建具有所需屬性的ColorStateList。然後將這些在EditText

final ColorStateList colors = new ColorStateList(
       new int[][]{ 
         new int[]{android.R.attr.state_selected}, 
         new int[]{-android.R.attr.state_selected} 
       }, 
       new int[]{selectedColorCode, defaultColor} 
     ); 

((EditText) view).setTextColor(colors); 
+0

我用selectedColorCode和defaultColor colorPrimary和colorPrimaryDark,但是我只看到colorPrimary應用,所以 什麼betweeen這一點,((EditText上)視圖)的差異.setTextColor(ContextCompat.getColor( getContext(),R.color.colorPrimary));我看到他們都只是在色彩主體 –

相關問題