数字选择器限制其他数字选择器

时间:2013-12-03 10:33:34

标签: android numberpicker

我有2个号码选择器,一个是小时,另一个是分钟。我希望可用的选项范围从1小时30分钟到5小时58分钟。一个号码选择器用于小时,另一个用于分钟

当Hours numnerpicker为1时,我想将其他数字选择器限制在30到58之间的值(以2为增量)。到目前为止我所尝试的并不起作用,而分数数字选择器只是归零并且不动。

// Create the array of numbers that will populate the numberpicker
    final String[] hours = {"1","2","3","4","5"};
    final String[] mins = new String[30];

    for(int i=0; i < 30; i++) {
        mins[i] = Integer.toString(i*2);
    }



    //set up number picker
    final NumberPicker nmHours = (NumberPicker) view.findViewById(R.id.hours);
    nmHours.setMaxValue(5);
    nmHours.setMinValue(1);
    nmHours.setWrapSelectorWheel(true);
    nmHours.setDisplayedValues(hours);
    //This gets the value stored in my global singleton 
    nmHours.setValue(Integer.parseInt(MethodLab.get(getActivity()).getSetup().getPealTime().substring(0,1)));
    nmHours.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

    final NumberPicker nmMins = (NumberPicker) view.findViewById(R.id.minutes);
    nmMins.setMaxValue(30);
    nmMins.setMinValue(1);
    nmMins.setWrapSelectorWheel(true);
    nmMins.setDisplayedValues(mins);
    //This gets the value stored in my global singleton 
    nmMins.setValue(Integer.parseInt(MethodLab.get(getActivity()).getSetup().getPealTime().substring(2,MethodLab.get(getActivity()).getSetup().getPealTime().length())));
    nmMins.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
    nmMins.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {

        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            // TODO Auto-generated method stub

            // TODO Auto-generated method stub
            if (nmHours.getValue() == 1){
                nmMins.setDisplayedValues(Arrays.copyOfRange(mins, 15, 30));
                nmMins.setMaxValue(30);
                nmMins.setMaxValue(15);
                nmMins.setValue(30);
            }
            else{
                nmMins.setDisplayedValues(mins);
                nmMins.setMaxValue(30);
                nmMins.setMinValue(1);
            }   
        }
    });

0 个答案:

没有答案