美文网首页
24_简单自定义NumberPicker

24_简单自定义NumberPicker

作者: Android_小生 | 来源:发表于2017-09-07 15:31 被阅读80次
示意图

使用默认数字选择器 NumberPicker 步骤

在布局文件中添加 NumberPicker 控件

<NumberPicker
        android:id="@+id/number_picker"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants"
        />

在代码中设置

numberPicker = (MyNumberPicker) findViewById(R.id.number_picker);
numberPicker.setMinValue(10); // 设置选择器的最小值
numberPicker.setMaxValue(20); // 设置选择器的最大值
numberPicker.setValue(12); // 设置选择器的当前值

自定义NumberPicker

有时候我们需要修改分割线或字体颜色等操作,那么我们就需要继承和重写 NumberPicker。

新建 MyNumberPicker 类

public class MyNumberPicker  extends NumberPicker{
    public MyNumberPicker(Context context) {
        super(context);
        setPickerDividerColor(this);
    }

    public MyNumberPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        setPickerDividerColor(this);
    }

    public MyNumberPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setPickerDividerColor(this);
    }

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

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

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

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

    public void updateView(View view) {
        if (view instanceof EditText) {
            // 修改字体的属性
            ((EditText)view).setTextColor(Color.parseColor("#BAA785"));
            ((EditText)view).setTextSize(32);
        }
    }

    // 通过反射拿到 mSelectionDivider 属性,然后设置上颜色值。
    private void setPickerDividerColor(NumberPicker mNumberPicker) {
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields) {
            if (pf.getName().equals("mSelectionDivider")) {
                pf.setAccessible(true);
                try{
                    pf.set(mNumberPicker,new ColorDrawable(Color.parseColor("#3F51B5")));
                }catch (IllegalAccessException e) {
                    e.printStackTrace();
                }catch (Resources.NotFoundException e) {
                    e.printStackTrace();
                }catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

在布局文件中使用

 <com.example.zhanghuabin.mynumberpicker.MyNumberPicker
        android:id="@+id/number_picker"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants"
        />

在代码中的使用和系统默认的方式一样。

相关文章

网友评论

      本文标题:24_简单自定义NumberPicker

      本文链接:https://www.haomeiwen.com/subject/upsvjxtx.html