Android NumberPicker 修改分割线颜色和高度及字体颜色大小

(1)重写NumberPicker已达到修改显示字体颜色大小

public class TextColorNumberPicker extends NumberPicker {
 
    public TextColorNumberPicker(Context context) {
        super(context);
    }
 
    public TextColorNumberPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public TextColorNumberPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
 
    @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);
    }
 
    public void updateView(View view) {
        if (view instanceof EditText) {
            //这里修改显示字体的属性,主要修改颜色
            ((EditText) view).setTextColor(Color.parseColor("#BAA785"));
        }
    }
}

(2)修改分割线颜色,需要在初始化控件后,通过反射进行动态设置颜色。

**********注意:在NumberPicker 的 setDisplayedValues调用后调用如下方法:

 1 private void setNumberPickerDividerColor(NumberPicker numberPicker) {
 2         NumberPicker picker = numberPicker;
 3         Field[] pickerFields = NumberPicker.class.getDeclaredFields();
 4         for (Field pf : pickerFields) {
 5             if (pf.getName().equals("mSelectionDivider")) {
 6                 pf.setAccessible(true);
 7                 try {
 8                     //设置分割线的颜色值 透明
 9                     pf.set(picker, new ColorDrawable(this.getResources().getColor(android.R.color.transparent)));
10                 } catch (IllegalArgumentException e) {
11                     e.printStackTrace();
12                 } catch (Resources.NotFoundException e) {
13                     e.printStackTrace();
14                 } catch (IllegalAccessException e) {
15                     e.printStackTrace();
16                 }
17                 break;
18             }
19         }
20     }

注意:修改分割线的高度:查看NumberPicker,找到mSelectionDividerHeight属性,修改:

 1 // 分割线高度
 2         for (Field pf : pickerFields) {
 3             if (pf.getName().equals("mSelectionDividerHeight")) {
 4                 pf.setAccessible(true);
 5                 try {
 6                     int result = 1;
 7                     pf.set(picker, result);
 8                 } catch (Exception e){
 9                     e.printStackTrace();
10                 }
11                 break;
12             }
13         }

 

(3)使用NumberPicker 注意:

1 // 禁止点击后打开键盘
2 picker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
3 // 设置最大值、最小值 已达到显示所有符合条件的数值
4 picker.setMaxValue(array.length - 1);
5 picker.setMinValue(0);

(4) List 转换为 Array[], 以便NumberPicker设置setDisplayedValues(String[]):

String[] array = new String[list.size()];
list.toArray(array);

连接:http://my.oschina.net/yaly/blog/532153

posted @ 2015-12-09 17:16  晕菜一员  阅读(8893)  评论(0编辑  收藏  举报