NumberPicker 和 NumberPickerView 数字选择器

吴升
2023-12-01

1,如果仅仅是用于数字 NumberPicker 更为方便

NumberPicker :

 <com.shawnlin.numberpicker.NumberPicker
        android:layout_gravity="center_horizontal"
        android:id="@+id/picker_height"
        android:layout_width="200dp"
        android:layout_height="250dp"
        android:layout_centerInParent="true"
        app:np_width="64dp"
        app:np_height="180dp"
        app:np_dividerColor="@color/colorPrimary"
        app:np_formatter="@string/number_picker_formatter"
        app:np_max="240"
        app:np_min="90"
        app:np_value="160"
        app:np_wheelItemCount="5"
        app:np_wrapSelectorWheel="false"
        app:np_selectedTextColor="@color/colorPrimary"
        app:np_selectedTextSize="@dimen/selected_text_size"
        app:np_textColor="@color/colorPrimary"
        app:np_textSize="@dimen/text_size"
        app:np_typeface="@string/roboto_light"

        />

  • 设置循环状态
WrapSelectorWheel( false );

  • 设置不可编辑
//设置不可编辑 numberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

  • 设置分割线颜色
   private void setNumberPickerDividerColor(NumberPicker numberPicker) {
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields) {
            if (pf.getName().equals("mSelectionDivider")) {
                pf.setAccessible(true);
                try {
                    //设置分割线的颜色值
                    pf.set(numberPicker, new ColorDrawable(getResources().getColor(R.color.numberpicker_divider_color)));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }

2. NumberPickerView 


 <cn.carbswang.android.numberpickerview.library.NumberPickerView
            android:id="@+id/picker"
            android:layout_width="wrap_content"
            android:layout_height="240dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:background="#ffffff"
            android:contentDescription="test_number_picker_view"
            app:npv_ItemPaddingHorizontal="5dp"
            app:npv_ItemPaddingVertical="5dp"
            app:npv_ShowCount="5"

            app:npv_RespondChangeOnDetached="false"
            app:npv_TextSizeNormal="16sp"
            app:npv_TextSizeSelected="20sp"
            app:npv_WrapSelectorWheel="true"/>


NumberPickerView 设置数字内容的话 要通过String[] display来设置

	NumberPickerView picker = (NumberPickerView) findViewById(R.id.picker);
        String[] display=new String[150];
        for(int i=90;i<240;i++){
            display[i-90]=i+"";
        }
        picker.refreshByNewDisplayedValues(display);





 类似资料: