Android 4. + NumberPicker - 强制数字键盘布局

时间:2013-11-17 17:20:11

标签: android numberpicker

发布此信息是为了帮助别人;但是,如果有更简单的方法来做同样的事情,我希望有人可以分享他们的步骤。如果我使用方法'setDisplayedValues'传递值数组以显示在NumberPicker中,底层方法强制使用文本键盘布局,而不是数字布局,这不是我想要的。 我找到了一种为NumberPicker设置InputType的方法。这是在我的扩展课程中完成的:

类公共类NumberPickerDialogFragment扩展DialogFragment

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // We are getting the parameters passed in to figure out the range of the Number Picker
    NumPickerValues = getArguments().getStringArray("NumPickerRange");
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View dlgNumPicker = inflater.inflate(R.layout.dialog_num_picker, null);
    NumberPicker np = (NumberPicker) dlgNumPicker.findViewById(R.id.npNumberPicker);
    // Always remember that NumberPicker methods need an index position, rather than the value in that array position
    np.setMinValue(0);
    np.setMaxValue(NumPickerValues.length-1);
    np.setDisplayedValues(NumPickerValues);
    np.setValue(getArguments().getInt("InitialValue"));
    np.setWrapSelectorWheel(false);
    np.setOnValueChangedListener(this);
    // Since the underlying code for the NumberPicker sets the keyboard layout for text, due to the use of 'setDisplayedValues',
    // we need to set it back to a number keyboard layout
    ((EditText)np.getChildAt(0)).setRawInputType(InputType.TYPE_CLASS_NUMBER);
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    return builder.setView(dlgNumPicker)
            .setTitle(getArguments().getInt("NumPickerTitle"))
            .setPositiveButton(getArguments().getInt("SaveButtonTitle"), new ButtonCLickListener())
            .setNegativeButton(R.string.cancel, new ButtonCLickListener())
            .create();
}

在上面的代码部分,设置InputType的行是 的((的EditText)np.getChildAt(0))setRawInputType(InputType.TYPE_CLASS_NUMBER);

我在网上找不到任何可以做到这一点的内容。这似乎是一种干净的方式;但是,我不确定这样做是否存在任何性能问题。如果有,请告诉我们。

0 个答案:

没有答案