I added a NumberPicker to my layout and debugged my app. Now I see that the NumberPicker doesn´t work, I can´t see any "+" or "-" button and also when I click on a number on my keyboard, nothing happens.

Here´s my layout file:

<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" />

Where´s the problem?

有帮助吗?

解决方案

Are you actually populating the number picker with values? As shown below?

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);

其他提示

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

Make sure that setMinValue and setMaxValue and setValue are being passed the indexes, not the real values. Here is one example, 0-500, skipping by 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
                    }
            );
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top