我有一个数字选择器 -
<NumberPicker
android:id="@+id/shopping_cart_holder_quantity_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/noto_sans"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
我正在尝试实现如下图所示的数字选择器 -
是的,这是在 iOS 中。我想知道如何在 Android 中实现相同的功能。
最佳答案
您只需使用 android 库即可实现此效果。我会建议 this一个。
只需将以下行添加到您的 gradle 文件中:
compile 'com.github.travijuu:numberpicker:1.0.7'
然后您可以在您的 .xml
文件中实现数字选择器,如下所示:
<com.travijuu.numberpicker.library.NumberPicker
android:id="@+id/number_picker"
android:layout_width="130dp"
android:layout_height="40dp"
numberpicker:min="0"
numberpicker:max="10"
numberpicker:value="-5"
numberpicker:unit="1"
numberpicker:focusable="false"
numberpicker:custom_layout="@layout/number_picker_custom_layout" />
您可以像这样自定义您的选择器:
NumberPicker numberPicker = (NumberPicker) findViewById(R.id.number_picker);
numberPicker.setMax(15);
numberPicker.setMin(5);
numberPicker.setUnit(2);
numberPicker.setValue(10);
关于android - 如何更改数字选择器以显示加号和减号按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59643449/