NumberPicker无法选择第一个值

时间:2016-12-07 11:40:19

标签: android numberpicker

嘿所以我正在使用NumberPicker,我的第一个值是"所有"这意味着要将EditText重置为0并且毕竟我有数字,问题是当我进入选择器并直接选择"所有"它只是没有做任何事情,它将NumberPicker保留在我选择的最后一个数字上,如果我想选择所有选项,我必须向上/向下滚动然后返回到"所有"并选择它..

我的代码如下所示:

  String[] values = new String[21];
        values[0] = "All";
        for (int i = 1; i < values.length; i++)
            values[i] = "" + (i + 15);
        NumberPicker agePicker = new NumberPicker(this);
        agePicker.setMinValue(15);
        agePicker.setMaxValue(15 + (values.length - 1));
        agePicker.setDisplayedValues(values);
        agePicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

        NumberPicker.OnValueChangeListener ageListener = new NumberPicker.OnValueChangeListener() {

            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                AgeFilterTemp = newVal;
            }
        };


        agePicker.setOnValueChangedListener(ageListener);
        AlertDialog.Builder builder = new AlertDialog.Builder(this).setView(agePicker);
        builder.setTitle("Maximum age");
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                if (AgeFilterTemp != 15 && AgeFilterTemp != 0) {
                    AgeFilter.setText("" + AgeFilterTemp);
                    setAgeFilter(AgeFilterTemp);
                } else {
                    AgeFilter.setText("Age");
                    adapter.setListData(listData);
                    recyclerView.setAdapter(adapter);

                }

            }
        });
        builder.show();

0 个答案:

没有答案