How to use NumberPicker from QuietlyCoding in pref

2019-03-04 23:04发布

I am trying to use QuietlyCoding NumberPicker because I haven´t found any other but I can´t get it working. I imported the library as a project and then I add it to my project:

Preference Activity:

public class MainPrefs extends PreferenceActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        PreferenceManager preferenceManager = getPreferenceManager();
        preferenceManager.setSharedPreferencesMode(MODE_PRIVATE);
        preferenceManager.setSharedPreferencesName("numberPicker.preferences");

        this.addPreferencesFromResource(R.xml.main_preferences);

        this.findPreference("SMSSentLimit").setOnPreferenceChangeListener(new OnPreferenceChangeListener(){
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                TrackerService.updateStats(Long.decode(newValue.toString()));
                return true;
            }
        });
    }

}

main_preference.xml

<PreferenceCategory android:title="General" >
    <EditTextPreference
        android:defaultValue="0"
        android:title="@string/SMSSentLimitTitle"
        android:key="SMSSentLimit"
        android:summary="@string/SMSSentLimitSummary" 
        android:inputType="number" />
    <com.michaelnovakjr.numberpicker.NumberPickerPreference
        android:key="demo.preference"
        android:title="Sample Number Picker"
        android:summary="Number picker as a preference"
        picker:defaultValue="15"
        picker:startRange="-50"
        picker:endRange="50" />
</PreferenceCategory>

I can´t compile my project on this way, to compile my project I have to edit my .xml on this way:

    <com.michaelnovakjr.numberpicker.NumberPickerPreference
        android:key="demo.preference"
        android:title="Sample Number Picker"
        android:summary="Number picker as a preference" />

Removing defaultValue, startRange and endRange, on this way my project is compiled and I can see my preference but when I click it I get force close because I didn´t define the values. Why I can add this values ? Have anyone worked whit this library ?

Thank you

1条回答
【Aperson】
2楼-- · 2019-03-04 23:49

You need to add the schema for the library attributes, in the top level of your preferences xml file. It should look something like this - the second/third lines are the important ones.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:picker="http://schemas.android.com/apk/res/[com.yourpackagename]">
    <PreferenceCategory android:title="General" >
        <EditTextPreference
            android:defaultValue="0"
            android:title="@string/SMSSentLimitTitle"
            android:key="SMSSentLimit"
            android:summary="@string/SMSSentLimitSummary" 
            android:inputType="number" />
        <com.michaelnovakjr.numberpicker.NumberPickerPreference
            android:key="demo.preference"
            android:title="Sample Number Picker"
            android:summary="Number picker as a preference"
            picker:defaultValue="15"
            picker:startRange="-50"
            picker:endRange="50" />
    </PreferenceCategory>
</PreferenceScreen>
查看更多
登录 后发表回答