Android(安卓)NumberPicker详细使用

Android NumberPicker详细使用

 

 

1.普通使用

 

1.1.布局

                    

 

1.2.Java代码

/**     * Android NumberPicker     * */    private void initNumberPicker() {        NumberPicker numberPicker = findViewById(R.id.activity_main_testnumberpicker);        //设置最大值        numberPicker.setMaxValue(150);        //设置最小值        numberPicker.setMinValue(50);        //设置当前值        numberPicker.setValue(105);        //设置滑动监听        numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {            //当NunberPicker的值发生改变时,将会激发该方法            @Override            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {                String toast = "oldVal:" + oldVal + "   newVal:" + newVal;                Toast.makeText(MainActivity.this, toast, Toast.LENGTH_SHORT).show();            }        });    }

 

1.3.效果

 

 

 

 

 

2.自定义NumberPicker(设置字体颜色,字体大小,分割线颜色)

 

2.1.自定义View

public class MyNumberPicker extends NumberPicker {    /**     * 构造方法 NumberPicker     * */    public MyNumberPicker(Context context) {        super(context);    }    public MyNumberPicker(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyNumberPicker(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    /**     * addView方法 ViewGroup     * */    @Override    public void addView(View child) {        super.addView(child);        setNumberPickerView(child);    }    @Override    public void addView(View child, int index) {        super.addView(child, index);        setNumberPickerView(child);    }    @Override    public void addView(View child, int width, int height) {        super.addView(child, width, height);        setNumberPickerView(child);    }    @Override    public void addView(View child, ViewGroup.LayoutParams params) {        super.addView(child, params);        setNumberPickerView(child);    }    @Override    public void addView(View child, int index, ViewGroup.LayoutParams params) {        super.addView(child, index, params);        setNumberPickerView(child);    }    public void setNumberPickerView(View view) {        if (view instanceof EditText) {            ((EditText) view).setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimary)); //字体颜色            ((EditText) view).setTextSize(20f);//字体大小        }    }}

 

2.2.布局

                    

 

 

2.3.Java代码

/**     * Android NumberPicker     * */    private void initNumberPicker() {        MyNumberPicker numberPicker = findViewById(R.id.activity_main_testnumberpicker);        //设置最大值        numberPicker.setMaxValue(150);        //设置最小值        numberPicker.setMinValue(50);        //设置当前值        numberPicker.setValue(105);        //关闭编辑模式        numberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);        //分割线颜色        setNumberPickerDividerColor(numberPicker);        //设置滑动监听        numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {            //当NunberPicker的值发生改变时,将会激发该方法            @Override            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {                String toast = "oldVal:" + oldVal + "   newVal:" + newVal;                Toast.makeText(MainActivity.this, toast, Toast.LENGTH_SHORT).show();            }        });    }    /**     * 自定义滚动框分隔线颜色     */    private void setNumberPickerDividerColor(NumberPicker number) {        Field[] pickerFields = NumberPicker.class.getDeclaredFields();        for (Field pf : pickerFields) {            if (pf.getName().equals("mSelectionDivider")) {                pf.setAccessible(true);                try {                    //设置分割线的颜色值                    pf.set(number, new ColorDrawable(ContextCompat.getColor(this, R.color.colorPrimary)));                } catch (Exception e) {                    e.printStackTrace();                }                break;            }        }    }

 

2.4.效果图