我正在为显示AM和PM值的自定义数字选择器。请检查以下代码。
<com.naushad.kenocustomer.util.NumberPicker
android:id="@+id/np_ampm"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_centerHorizontal="true"/>
XML代码看起来像这样,下面是Java代码。
final String ampm[] = {"AM", "PM"};
datetimeBinding.npAmpm.setFormatter(R.string.number_picker_formatter);
datetimeBinding.npAmpm.setSelectedTextColor(Color.parseColor("#09bcf2"));
datetimeBinding.npAmpm.setTextColor(Color.parseColor("#babdc2"));
datetimeBinding.npAmpm.setDefaultSelectedTextSize(20);
datetimeBinding.npAmpm.setTextSize(getResources().getDimension(R.dimen.fifteen));
datetimeBinding.npAmpm.setMinValue(1);
datetimeBinding.npAmpm.setMaxValue(ampm.length);
datetimeBinding.npAmpm.setDisplayedValues(ampm);
int isAM = c.get(Calendar.AM_PM);
int myValue = Arrays.asList(ampm).indexOf(String.valueOf(isAM));
if(isAM == 0)
datetimeBinding.npAmpm.setValue(Integer.parseInt(ampm[1].toString()));
else
datetimeBinding.npAmpm.setValue(Integer.parseInt(ampm[1].toString()));
现在,我将按照当前时间动态设置数字拾取器值。但是我已经按照以下方式编写了代码并获得 number Grord exception。
我想根据当前时间将AM PM值设置为数字拾取器。请建议我如何实现。谢谢..
我猜你在这里的某个地方遇到错误?
if(isAM == 0)
datetimeBinding.npAmpm.setValue(Integer.parseInt(ampm[1].toString()));
else
datetimeBinding.npAmpm.setValue(Integer.parseInt(ampm[1].toString()));
这似乎是合理的,因为ampm[1]
包含字符串" am"和
Integer.parseInt(ampm[1].toString())
您尝试将其解析到整数中,但" AM"不是数字,而是字符串。因此
java.lang.NumberFormatException: For input string: "PM"
例外是完全合理的。如果您想显示它作为字符串去除Integer.parseInt(...)
。我不知道您的datetimeBinding.npAmpm
到底是什么,但是如果它是这样的标签设置?
datetimeBinding.npAmpm.setValue(ampm[1]);
(另外:在if(isAM == 0)
的两种情况下,您都使用ampm[1]
,我想其中一个应该是ampm[0]
(