如何在首选项中使用 QuietlyCoding 中的 NumberPicker?

How to use NumberPicker from QuietlyCoding in preferences?(如何在首选项中使用 QuietlyCoding 中的 NumberPicker?)

本文介绍了如何在首选项中使用 QuietlyCoding 中的 NumberPicker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 QuietlyCoding NumberPicker,因为我没有找到任何其他工具,但我无法让它工作.我将库作为项目导入,然后将其添加到我的项目中:

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:

偏好活动:

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;
            }
        });
    }

}

ma​​in_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>

我无法以这种方式编译我的项目,要编译我的项目,我必须以这种方式编辑我的 .xml:

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" />

删除 defaultValue、startRange 和 endRange,这样我的项目就被编译了,我可以看到我的偏好,但是当我点击它时,我会强制关闭,因为我没有定义这些值.为什么我可以添加这个值?有人用过这个库吗?

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 ?

谢谢

推荐答案

您需要在首选项 xml 文件的顶层添加库属性的架构.它应该看起来像这样 - 第二/第三行是重要的.

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>

这篇关于如何在首选项中使用 QuietlyCoding 中的 NumberPicker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在首选项中使用 QuietlyCoding 中的 NumberPicker?

基础教程推荐