我正在构建一个简单的自定义对话框,供用户使用两个NumberPickers(它们表示权重的整数和小数部分)输入其当前权重。这些位于并排侧在LinearLayout中Android的NumberPicker在屏幕方向更改时显示错误值
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal">
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/newweight"
android:layout_gravity="center_horizontal"
android:background="@color/primary_dark"/>
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/newweightdecimal"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="1pt"
android:background="@color/primary_dark"/>
</LinearLayout>
我注意到,屏幕方向的变化和活性重建后,第一NumberPicker始终显示任何数字,第二个节目。
奇怪的是,当它的值以编程方式访问时,它会返回正确的值。此外,在numberpicker(向上或向下列表)上选择+或 - 可使NumberPicker再次显示正确的值。
例如,如果在方向改变之前第一个显示70,第二个显示1,之后两者都显示1.然而,它的getValue返回70.同样,如果我在第一个数字选择器上选择+,它将其值更改为71,正如你所期望的那样,如果它首先显示70。
以下是Activity的onCreate方法中两个NumberPickers的初始化。
NumberPicker newweight = (NumberPicker) findViewById(R.id.newweight);
newweight.setMinValue(0);
newweight.setMaxValue(250);
newweight.setValue((int)Math.round(Math.floor(weight)));
newweight.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
NumberPicker newweightdecimal = (NumberPicker) findViewById(R.id.newweightdecimal);
newweightdecimal.setMinValue(0);
newweightdecimal.setMaxValue(9);
newweightdecimal.setValue((int)Math.round(10*(weight - Math.floor(weight))));
newweightdecimal.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
有没有人有一个想法,为什么发生这种情况,以及如何确保NumberPicker显示正确的值?
感谢
PLZ标记答案权利...如果它对你来说是可行的... :) –
这是有帮助的,但它只是防止在方向变化上重新创建活动。在这个特定的场景中,我对肖像和风景都使用相同的布局,所以我想它可以为我工作。不过,我仍然想知道为什么发生这种奇怪的事情,因为它非常令人沮丧。当然,如果没有人会有任何其他的想法,我会标记这个答案是正确的:) – rianor
谢谢... @rianor ...从我的角度来看,..防止活动重新创建,因此数据保持相同的两个方向手机(横向或纵向)。 –