Display more numbers in NumberPicker

♀尐吖头ヾ 提交于 2019-12-12 19:42:08

问题


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.



来源:https://stackoverflow.com/questions/49790141/display-more-numbers-in-numberpicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!