1

我发布此内容希望对其他人有所帮助;但是,如果有更简单的方法可以做到这一点,我希望有人可以分享他们的步骤。如果我使用方法“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);

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

4

0 回答 0