PreferenceActivity: save value as integer

前端 未结 5 663
一向
一向 2020-12-12 17:00

Using a simple EditTextPreference in my preferences activity:



        
相关标签:
5条回答
  • 2020-12-12 17:38

    Even if you set android:numeric="integer" it'll be text preference - as its name suggest. You could easily convert string value to int using Integer.valueOf(). Also you could overwrite PreferenceActivity to do conversion automatically on exit.


    I think the best solution is to write simple method to get this value from preferences. Something like:

    public static int getSomePref(Context context) {
        SharedPreferences prefs =
            PreferenceManager.getDefaultSharedPreferences(context);
        String value = prefs.getString("SomeKey", null);
        return value == null ? -1 : Integer.valueOf(value);
    }
    

    Then you could very easily use it from your code.

    0 讨论(0)
  • 2020-12-12 17:39

    I think this is the shortest one I could come up with:

    int CheckInterval = Integer.parseInt(sharedPreferences.getString("check_frequency","60"));
    
    0 讨论(0)
  • 2020-12-12 17:48

    Even though an Answer has been parked accepted I would like to share one more shorter way to achieve this :

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    int value = Integer.parseInt(preferences.getString("SomeKey", "-1"));
    

    Since you have already set that only numbers can be entered this won't through any exception. yet to complete my answer :

    <EditTextPref
        android:key="SomeKey"
        android:title="@string/some_title"
        android:summary="..."
        android:numeric="integer"
        android:maxLength="2" />
    
    0 讨论(0)
  • 2020-12-12 17:49

    I had the same Problem. (I wanted SharedPreference to give me a port number that i stored in a preferences xml file as defaultValue).

    Implementing all the SharedPreferences methods would be much effort, so writing a custom method in the class that instanced the SharedPreferences, as broot suggested would be best i think.

    You can aswell just use the Static method of Integer in the line where you need it:

    int number = Integer.valueOf(settings.getString("myNumberString", "0"));
    
    0 讨论(0)
  • 2020-12-12 17:50

    You could extend EditTextPreference:

    public class IntEditTextPreference extends EditTextPreference {
    
        public IntEditTextPreference(Context context) {
            super(context);
        }
    
        public IntEditTextPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public IntEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected String getPersistedString(String defaultReturnValue) {
            return String.valueOf(getPersistedInt(-1));
        }
    
        @Override
        protected boolean persistString(String value) {
            return persistInt(Integer.valueOf(value));
        }
    }
    

    It would be better to overwrite onSetInitialValue() and setText() methods, but then you would have to copy some code from a base class. Above solution is simplier, but it's quite tricky - "string" methods do something with ints. Try to not extend this class further ;-)

    You could use it from XML by:

    <package.name.IntEditTextPreference
        android:key="SomeKey"
        android:title="@string/some_title"
        android:summary="..."
        android:numeric="integer"
        android:maxLength="2"
    />
    
    0 讨论(0)
提交回复
热议问题