android - 设置NumberPicker的增减间隔

标签 android

是否可以将NumberPicker的增减间隔设置为其他值?例如到 50(50、100、150、200、250 等)?还是需要一些定制/修改?

最佳答案

我认为你可以这样做:

  • 初始化 displayed array values对于您的选择器:

    int NUMBER_OF_VALUES = 10; //num of values in the picker
    int PICKER_RANGE = 50;
    ...
    String[] displayedValues  = new String[NUMBER_OF_VALUES];
    //Populate the array
    for(int i=0; i<NUMBER_OF_VALUES; i++)
        displayedValues[i] = String.valueOf(PICKER_RANGE * (i+1));
    /* OR: if the array is easy to be hard-coded, then just hard-code it:
       String[] displayedValues = {"50", "100", "150", .....}; */
    
  • 将显示的数组设置为您的选择器:

    /* 
    NOTE: Your picker range has to be [0, displayedValues.size()-1] to be 
          consistent with the array index (displayedValues index).
    */
    //ToDo: Initialize the picker first.
    numPicker.setMinValue(0); 
    numPicker.setMaxValue(displayedValues.size()-1);
    numPicker.setDisplayedValues(displayedValues);
    
  • 当您想获取/设置选择器的值时:

    //To get the current value in the picker
    choosenValue = displayedValues[numPicker.getValue()]; 
    //To set a new value (let's say 150)
    for( int i=0; i<displayedValues.length ; i++ )
        if( displayedValues[i].equals("150") )
             numPicker.setValue(i);
    

最后,有一个很棒的易于定制的小部件,叫做 android-wheel .您可以查看并根据需要使用它。

关于android - 设置NumberPicker的增减间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11925681/

相关文章:

android - 如何设置应用:tabBackground of a tabLayout programmatically?

android - 获取 Apple/Google ID 以替换用户帐户中的电子邮件地址 - 可行性和意义?

android - 什么是 Android 服务

android - 如何在点击 EditText 时选择整个文本

android - 使用 BroadcastReceiver 在 Android 中获取断开连接的 WiFi 网络的 SSID?

android - 有什么方法可以通过双击自动访问 Logcat 中的任何登录?

java - Play Billing Library 对话框关闭时有没有办法收听?

android - 应用程序关闭时自动启动服务

android - 如何在构建 Android App Bundle 时指定环境变量

java - 初学者无法创建第一个 Android 应用程序项目