【问题标题】:Android: NumberPicker doesn´t workAndroid:NumberPicker 不起作用
【发布时间】:2013-06-02 19:36:14
【问题描述】:

我在布局中添加了一个 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" />

问题出在哪里?

【问题讨论】:

    标签: android layout numberpicker


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      数字选择器没有+- 更多地将其视为滚轮。您必须设置最小数量和最大数量才能真正显示任何内容

      setMinValue(5);
      setMaxValue(120);
      

      如果你点击选择器的顶部,它会增加数字,如果你点击选择器的底部,它会减少数字

      【讨论】:

        【解决方案3】:

        确保将 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
                            }
                    );
        

        【讨论】:

          猜你喜欢
          • 2013-06-18
          • 2016-03-13
          • 1970-01-01
          • 1970-01-01
          • 2016-09-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-28
          相关资源
          最近更新 更多