Display more numbers in NumberPicker

2019-07-21 11:45发布

问题:

I have two issues, first one is removing the divider in the NumberPicker and I'm extending the NumberPicker in Android to solve this issue, like this:

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.widget.NumberPicker;

import java.lang.reflect.Field;


public class ExtendedNumberPicker extends NumberPicker {

    public ExtendedNumberPicker(Context context, AttributeSet attrs) {
        super(context, attrs);

        Class<?> numberPickerClass = null;
        try {
            numberPickerClass = Class.forName("android.widget.NumberPicker");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Field selectionDivider = null;
        try {
            selectionDivider = numberPickerClass.getDeclaredField("mSelectionDivider");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        try {
            selectionDivider.setAccessible(true);
            selectionDivider.set(this, null);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

My other issue is that I didn't find any way to display more than 3 elements like the below UI:

回答1:

Set Removing divider lines

<NumberPicker
   ...
   android:theme="@style/DefaultNumberPickerTheme" />

style.xml

<style name="DefaultNumberPickerTheme" parent="AppTheme">
        <item name="colorControlNormal">@color/transparent</item>
</style>

For the other part : Checkout this question

Hope this helps !



回答2:

For your second part of the question please look at my answer here , you will find easy way to display more than 3 elements.