2

当我的偏好之一被点击时,我试图打开一个网站(而不是当偏好实际改变时,因为没有一个)。

问题是onPreferenceClick()从未调用过。

这是我的PreferenceActivity

public class About extends PreferenceActivity implements
        OnPreferenceClickListener {

TextView tv_developer;
TextView tv_version;
String versionName;

int counter = 0;

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

        addPreferencesFromResource(R.xml.about);        

        Preference p_developer = (Preference) findPreference("p_developer");
        p_developer.setOnPreferenceClickListener(this);

        Preference p_licences = (Preference) findPreference("p_licences");
        p_licences.setOnPreferenceClickListener(this);



    }

    @Override
    public boolean onPreferenceClick(Preference pref) {
        // TODO Auto-generated method stub

        Log.i("Anything pressed", "YES");
        if (pref.getKey().equals("p_developer")) {
            Log.i("p_developer", "YES");


        } else {
            Log.i("p_developer", "NO");
        }
        return true;
    }   
}

这是xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="about"
    android:title="About" >


    <Preference
        style="?android:preferenceInformationStyle"
        android:key="p_licences"
        android:title="p_licences" />

    <PreferenceCategory android:title="Category" >

        <Preference
            style="?android:preferenceInformationStyle"
            android:key="p_developer"
            android:title="p_developer" />

    </PreferenceCategory>

</PreferenceScreen>

我还发现这也无济于事:

<Preference android:title="@string/prefs_web_page" >
    <intent android:action="android.intent.action.VIEW"
            android:data="http://www.example.com" />
</Preference>
4

4 回答 4

3

添加android:enabled="true"Preference

<Preference
    style="?android:preferenceInformationStyle"
    android:key="p2"
    android:enabled="true"
    android:title="p2" />
于 2013-09-14T10:15:29.183 回答
1

您正在使用:

 PreferenceActivity.addPreferencesFromResource(R.layout.about);

不应该更像:

 PreferenceActivity.addPreferencesFromResource(R.xml.yourxmlfilename);

由于您定义的首选项不是布局。 您需要加载 .xml 文件的名称,而不是 .xml 中的首选项键。

因此,将您的首选项 .xml 文件放在“xml”文件夹中,并为其命名,如“mypreferences.xml”。然后,像这样加载它:

PreferenceActivity.addPreferencesFromResource(R.xml.mypreferences);

在我的项目中,文件如下所示:(名称“settings.xml”)

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="Your Settings" >
            <Preference
            android:key="delete"
            android:title="Delete User"
            android:summary="Deletes the user." />

      <!-- and so on -->

我通过调用将它添加到 PreferenceActivity:

PreferenceActivity.addPreferencesFromResource(R.xml.settings);

设置 onClickListener:

Preference deletePref = (Preference) findPreference("delete");
        deletePref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                // do stuff
                return true;
            }
        });

它有效。

此外,您可以尝试为标题和键使用不同的名称。

于 2013-09-03T09:29:26.230 回答
0

新鲜的答案在这里。

您需要先声明侦听器。然后为每个(!)结束 Preference 对象附加该侦听器。像这些:

    final static int CAMERA_SLEEP_TIME=0,
                CAMERA_TIME=1,
                RESET=2;

private Preference.OnPreferenceClickListener listener =new Preference.OnPreferenceClickListener(){
    @Override
    public boolean onPreferenceClick(Preference preference) {
        String keyLcl = preference.getKey();
       if(keyLcl.equals(cameraSleepTime))
           triggerPrefsDialog(CAMERA_SLEEP_TIME);
        else
       if (keyLcl.equals(cameraTime))
           triggerPrefsDialog(CAMERA_TIME);
        else
       if (keyLcl.equals(reset))
           triggerPrefsDialog(RESET);
        else
       return false;
        return true;

    }


};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    Resources resLcl = getResources();
    cameraSleepTime=resLcl.getString(R.string.camera_sleep_time);
    cameraTime=resLcl.getString(R.string.camera_time);
    reset =resLcl.getString(R.string.reset);
    PreferenceScreen sceenLcl = getPreferenceScreen();
    PreferenceCategory pcLcl=(PreferenceCategory)sceenLcl.getPreference(1);
    pcLcl.getPreference(0).setOnPreferenceClickListener(listener);
    pcLcl.getPreference(1).setOnPreferenceClickListener(listener);
    pcLcl=(PreferenceCategory)sceenLcl.getPreference(2);
    pcLcl.getPreference(0).setOnPreferenceClickListener(listener);
    l.a(pcLcl.getTitle());
}

以及相关的preferences.xml:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceCategory

    android:title="@string/switch_mode">

    <CheckBoxPreference
        android:key="@string/enable_switch"
        android:defaultValue="true"
        android:gravity="center"
        android:title="@string/is_switched"

        />
</PreferenceCategory>
<PreferenceCategory

    android:title="@string/seconds_category">
    <Preference
        android:key="@string/camera_time"

        android:defaultValue="600000"
        android:title="@string/camera_time"
        />
    <Preference
        android:key="@string/camera_sleep_time"
        android:defaultValue="600000"
        android:title="@string/camera_sleep_time"
        />
</PreferenceCategory>
<PreferenceCategory
    android:title="@string/System">
    <Preference
        android:key="@string/reset"
        android:defaultValue="false"
        android:title="@string/reset"
        />
</PreferenceCategory>
<PreferenceCategory
    android:title="@string/to_be_added">
</PreferenceCategory>

于 2016-08-16T17:04:34.733 回答
0

对我来说,解决方案是扩展现有PreferenceCategory类(在支持库中)并覆盖isEnabled(). 这个方法总是返回false,所以我创建了一个名为的类ClickablePreferenceCategory并将其更改为true.

除此之外,我必须添加android:selectable="true"到我的自定义 Preference xml 节点。结果如下所示:

<PreferenceCategory
    android:enabled="true"
    android:key="@string/key_category_problem_solving"
    android:selectable="true"
    android:title="@string/problem_solving">

</PreferenceCategory>

ClickablePreferenceCategory

public class ClickablePreferenceCategory extends PreferenceCategory {

    public ClickablePreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public ClickablePreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ClickablePreferenceCategory(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ClickablePreferenceCategory(Context context) {
        super(context);
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
于 2016-12-06T13:45:03.890 回答