13

我在布局中添加了一个 NumberPicker 并调试了我的应用程序。现在我看到 NumberPicker 不起作用,我看不到任何“+”或“-”按钮,而且当我点击键盘上的数字时,什么也没有发生。

这是我的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#99cc00"
tools:context=".Gewichtsentwicklung" >

<NumberPicker
    android:id="@+id/numberPicker1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="80dp" />

<TextView
    android:id="@+id/tvGewichtUeberschrift"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_centerHorizontal="true"
    android:textColor="@color/white"
    android:textSize="13pt"
    android:layout_alignParentTop="true"
    android:text="Aktuelles Gewicht eingeben" />

<Button
    android:id="@+id/btGewicht"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/numberPicker1"
    android:layout_marginTop="79dp"
    android:text="Gewicht speichern" />

问题出在哪里?

4

3 回答 3

36

您实际上是在用值填充数字选择器吗?如下所示?

NumberPicker np = (NumberPicker) findViewById(R.id.numberPicker1);
String[] nums = new String[20];
for(int i=0; i<nums.length; i++)
       nums[i] = Integer.toString(i);

np.setMinValue(1);
np.setMaxValue(20);
np.setWrapSelectorWheel(false);
np.setDisplayedValues(nums);
np.setValue(1);
于 2013-06-06T17:14:07.590 回答
5

the number picker does not have a + or - think of it as more of a scroll wheel. you have to set the minimum number and the maximum number for it to show anything really

setMinValue(5);
setMaxValue(120);

if you click the top portion of the picker it increases the number, if you click the bottom portion of the picker it decreases the number

于 2013-06-06T17:15:45.277 回答
1

确保将 setMinValue 和 setMaxValue 和 setValue 传递给索引,而不是实际值。这是一个示例,0-500,跳过 10:

            NumberPicker number_picker = mRootView.findViewById(R.id.number_picker);
            String[] values = new String[51];
            for (int i = 0; i < values.length; i++) {
                String number = Integer.toString(i * 10);
                values[i] = number;
            }
            number_picker.setMinValue(0);
            number_picker.setMaxValue(50);
            number_picker.setWrapSelectorWheel(false);
            number_picker.setDisplayedValues(values);
            number_picker.setValue(20);

            number_picker.setOnValueChangedListener((picker, oldVal, newVal) ->
                    {
                      //update with newVal
                    }
            );
于 2018-11-12T15:25:35.483 回答