How does one declare the type of an Android preference?

后端 未结 3 503

I have a preferences.xml that looks like this:




        
3条回答
  •  余生分开走
    2021-01-01 11:07

    In your preferences xml you can add an option android:numeric with the value "integer". This way the user should only be able to enter a valid integer value.

    When loading the setting you should try to parse it to a number yourself (as all values are stored as Strings (@mbaird below)):

    try {
      float val = Float.parseFloat(sp.getString("sample", "3.0f"));
    } catch (NumberFormatException e) {
      // "sample" was not an integer value
      // You should probably start settings again
    }
    

提交回复
热议问题