崩溃的NumberPicker,改变了键盘类型

时间:2017-08-10 20:05:20

标签: android

当我输入第一个数字时,当我将numberPicker上的键盘类型更改为“TYPE_CLASS_NUMBER”时,我的Android应用程序会一直晃动。相同的工作没有指定键盘类型。所以看起来键盘改变就是问题所在。 这是代码:

public void chooseAmps(View view) {
    final NumberPicker numberPicker = new NumberPicker(this);
    NumberPicker.Formatter formatter = new PickerFormatter();
    //numberPicker.setFormatter(formatter);
    numberPicker.setMinValue(0);
    numberPicker.setMaxValue(100);
    numberPicker.setDisplayedValues(getStringArray(0, 10));
    enableNumberPickerManualEditing(numberPicker);


    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(R.string.AmpsMessage)
            .setTitle(R.string.AmpsTitle)
            .setPositiveButton(R.string.accept, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.d("UI", String.valueOf(numberPicker.getValue()));
                    double currentDesired = numberPicker.getValue() / 10.0;
                    charger.setCurrentDesired(currentDesired);
                    Log.d("UI", String.valueOf(charger.getCurrentDesired()));
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            })
            .setView(numberPicker);
    AlertDialog dialog = builder.create();
    dialog.show();
}

String[] getStringArray(double begin, double end) {
    String[] stringArray = new String[(int) ((end - begin) * 10)+1];
    double number = begin;
    for (int i = 0; i < (int) ((end - begin) * 10+1); i++) {
        stringArray[i] = String.format("%.1f", number);
        number += 0.1;
    }
    Log.d("UI", String.valueOf(number));
    return stringArray;
}

public static void enableNumberPickerManualEditing(NumberPicker numPicker) {
    int childCount = numPicker.getChildCount();

    for (int i = 0; i < childCount; i++) {
        View childView = numPicker.getChildAt(i);

        if (childView instanceof EditText) {
            EditText editText = (EditText) childView;
            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
            return;
        }
    }
}

}

此处的崩溃讯息:

  

“处理:com.jacobi.mario.charger,PID:28169                                                                            java.lang.IndexOutOfBoundsException:setSpan(3 ... 3)结束   长度2                                                                                 在   android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1320)                                                                                 在   android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:683)                                                                                 在   android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:676)                                                                                 在android.text.Selection.setSelection(Selection.java:78)                                                                                 在android.widget.EditText.setSelection(EditText.java:96)                                                                                 在   android.widget.NumberPicker $ SetSelectionCommand.run(NumberPicker.java:2278)                                                                                 在android.os.Handler.handleCallback(Handler.java:751)                                                                                 在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                 在android.os.Looper.loop(Looper.java:154)                                                                                 在android.app.ActivityThread.main(ActivityThread.java:6236)                                                                                 at java.lang.reflect.Method.invoke(Native Method)                                                                                 在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:891)                                                                                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)“

有人给我一个小费?

1 个答案:

答案 0 :(得分:0)

哇我刚刚找到了一个解决方案,但也许有更好的解决方案?

对我来说,它改变了: editText.setInputType(InputType.TYPE_CLASS_NUMBER); 至 editText.setRawInputType(InputType.TYPE_CLASS_NUMBER);