更改数字选择器对话框android的滚动方向

时间:2014-11-12 05:30:15

标签: android dialog scroll numberpicker

我在对话框中使用数字选择器,并希望将滚动方向从“上”更改为“向下”。这意味着目前默认情况下,如果我向上滚动,数字来自底部,但我希望它们来自上行,滚动将向下而不是向上。这是我的号码选择器对话框代码。

private static void getMeasure(int textMsg, final BoardRect item,
            final int defaultValue, final int maxValue,
            final OnUIMeasureReadListener listener) {
        final NumberPicker picker = new NumberPicker(
                AppContext.getActivityContext());
        picker.setMinValue(-1);
        picker.setMaxValue(maxValue);
        picker.setWrapSelectorWheel(false);
        picker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

        // create actual dialog
        final AlertDialog.Builder msgbox = new AlertDialog.Builder(
                AppContext.getActivityContext());

        msgbox.setCancelable(true);
        msgbox.setTitle(AppContext.getActivityContext().getResources()
                .getString(R.string.rect_dimen));
        msgbox.setMessage(textMsg);
        msgbox.setView(picker);

        msgbox.setPositiveButton(AppContext.getActivityContext().getResources()
                .getString(R.string.dlg_positive_btn),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            listener.measureRead(picker.getValue());
                        } catch (Exception ex) {
                        }
                    }
                });     
        AlertDialog dialog = msgbox.create();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();

        wmlp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
        wmlp.x = 135; // x position
        wmlp.y = 0; // y position
        dialog.getWindow().setAttributes(wmlp);
        dialog.show();
        dialog.getWindow().setLayout(350, 650);
    }

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。我使用setDisplayedValues()类的NumberPicker方法显式设置要显示的值。您可以生成一个字符串数组,表示所需数字的字符串值:

public String[] getDisplayValues(int minimumInclusive, int maximumInclusive) {

    ArrayList<String> result = new ArrayList<String>();

    for(int i = maximumInclusive; i >= minimumInclusive; i--) {
        result.add(Integer.toString(i));
    }

    return result.toArray(new String[0]);
}

将该数组存储在字段_displayValues中,然后您可以调用:

picker.setDisplayValues(_displayValues);
//we want the max value to be the index of our last value
picker.setMaxValue(_displayValues.length - 1);

引发OnValueChangeListener事件时,使用newVal作为数组的索引:

var realValue = Integer.parseInt(_displayValues[newVal]);

希望有所帮助。