addPreferencesFromResource 使共享首选项不起作用

发布于 2024-12-18 09:51:05 字数 5099 浏览 2 评论 0原文

所以可能是另一个新问题:)

我有一个Activity、一个AppWidgetProvider和一个PreferenceActivity。在 PreferenceActivity 中,我有一个 ListPreference,我用它来定义小部件的文本大小。当主活动启动时,sharedPreferences 加载没有问题,但是一旦 PreferenceActivity 运行,我就无法再检索 sharedPreference在主要活动中正确地价值观。我尝试直接在 PreferenceActivity 中检索 sharedPreference 值,并且发生了同样的事情:出于某种原因,当我尝试从 sharedPreferences 获取值时在我调用 addPreferencesFromResource(R.drawable.settings); (在评论“测试 1”处)之前,它说:

“onStart() 1,尺寸:小”和“小作品!”

即它有效。但是当我尝试在资源调用之后检索值(在注释“测试 2”处)时,它说:

“onStart() 2,大小:小”和“它不起作用......”

即它不起作用。我不知道为什么。显然,它获取了 sharedPreference 的值,因为它表示当前 textWidgetSize 在两种情况下都等于“小”,但由于某种原因,它并不认为“小” = 资源调用后的“小”。你们知道出了什么问题吗?

这是 PreferenceActivity 代码:

package dk.mfoller.android.basicnote;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.Toast;
import dk.mfoller.android.basicnote.R;

public class BasicNoteSettings extends PreferenceActivity{

    String widgetTextSize = "small";
    boolean widgetLineCounter = true;

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

        // Calls a function to get the preferences
        getPrefs();

        // Test 1
        makeToast("onStart() 1, size: " + widgetTextSize);
        if(widgetTextSize == "small") {
            makeToast("Small works!");
        } else if(widgetTextSize == "medium") {
            makeToast("Medium works!");
        } else if(widgetTextSize == "large") {
            makeToast("Large works!");
        } else {
            makeToast("It doesn't work ...");
        }

        // Gets the preference layout from xml
        addPreferencesFromResource(R.drawable.settings);

        // Calls a function to get the preferences
        getPrefs();

        // Test 2
        makeToast("onStart() 2, size: " + widgetTextSize);
        if(widgetTextSize == "small") {
            makeToast("Small works!");
        } else if(widgetTextSize == "medium") {
            makeToast("Medium works!");
        } else if(widgetTextSize == "large") {
            makeToast("Large works!");
        } else {
            makeToast("It doesn't work ...");
        }

    }

    // A function to get the preferences    
    private void getPrefs() {
        // Gets data from the shared preferences
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        widgetTextSize = prefs.getString("text_size_list", "small");
        widgetLineCounter = prefs.getBoolean("line_counter_cbox", true);
    }

    // A function to display a popup
    private void makeToast(String popup) {
        Toast.makeText(this, popup, Toast.LENGTH_SHORT).show();
    }

}

这是 xml 文档:

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

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="basicnote_settings"
    android:title="@string/settings_label">

    <PreferenceCategory android:title="Text size">
        <ListPreference android:key="text_size_list"
        android:title="Widget text size"
        android:summary="@string/text_size_summary"
        android:entries="@array/text_size_options"
        android:entryValues="@array/text_size_values"
        android:defaultValue="small" />
    </PreferenceCategory>

    <PreferenceCategory android:title="Other settings">
        <CheckBoxPreference android:key="line_counter_cbox"
            android:title="Line counter"
            android:summary="@string/hidden_lines_summary"
            android:defaultValue="true" />
    </PreferenceCategory>

</PreferenceScreen>

这是我的资源字符串:

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

<!-- Defines various resources -->
<resources>
    <string name="app_name">basicNote</string>
    <string name="note_hint">Tap to add some notes ...</string>
    <string name="fake_load">Loading notes ...</string>

    <string name="settings_label">basicNote settings</string>

    <string name="text_size_summary">Set the size of the widget text</string>
    <string-array name="text_size_options">
        <item>Small (12sp)</item>
        <item>Medium (13sp)</item>
        <item>Large (15sp)</item>
    </string-array>
    <string-array name="text_size_values">
        <item>small</item>
        <item>medium</item>
        <item>large</item>
    </string-array>

    <string name="hidden_lines_summary">Show/hide the number of lines not shown in the widget</string>
</resources>

So probably another newbish question :)

I have an Activity, an AppWidgetProvider and a PreferenceActivity. In the PreferenceActivity I have a ListPreference which I use to define the text-size for the widget. The sharedPreferences are loaded with no problems when the main activity starts, but as soon as the PreferenceActivity is run, I can no longer retrieve the sharedPreference values properly in the main activity. I tried retrieving the sharedPreference values directly in the PreferenceActivity and the same thing happened: For some reason when I try to get the values from the sharedPreferences BEFORE I call addPreferencesFromResource(R.drawable.settings); (at comment 'Test 1'), it says:

"onStart() 1, size: small" and "Small works!"

I.e. it works. But when I try to retrieve the values AFTER the resource-call (at comment 'Test 2'), it says:

"onStart() 2, size: small" and "It doesn't work ..."

I.e. it DOESN'T work. I have NO idea why. Obviously it gets the value of the sharedPreference as it says that the current textWidgetSize equals "small" in both cases, but for some reason it doesn't think that "small" = "small" after the resource-call. Do you guys have any idea what's wrong?

Here's the PreferenceActivity code:

package dk.mfoller.android.basicnote;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.Toast;
import dk.mfoller.android.basicnote.R;

public class BasicNoteSettings extends PreferenceActivity{

    String widgetTextSize = "small";
    boolean widgetLineCounter = true;

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

        // Calls a function to get the preferences
        getPrefs();

        // Test 1
        makeToast("onStart() 1, size: " + widgetTextSize);
        if(widgetTextSize == "small") {
            makeToast("Small works!");
        } else if(widgetTextSize == "medium") {
            makeToast("Medium works!");
        } else if(widgetTextSize == "large") {
            makeToast("Large works!");
        } else {
            makeToast("It doesn't work ...");
        }

        // Gets the preference layout from xml
        addPreferencesFromResource(R.drawable.settings);

        // Calls a function to get the preferences
        getPrefs();

        // Test 2
        makeToast("onStart() 2, size: " + widgetTextSize);
        if(widgetTextSize == "small") {
            makeToast("Small works!");
        } else if(widgetTextSize == "medium") {
            makeToast("Medium works!");
        } else if(widgetTextSize == "large") {
            makeToast("Large works!");
        } else {
            makeToast("It doesn't work ...");
        }

    }

    // A function to get the preferences    
    private void getPrefs() {
        // Gets data from the shared preferences
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        widgetTextSize = prefs.getString("text_size_list", "small");
        widgetLineCounter = prefs.getBoolean("line_counter_cbox", true);
    }

    // A function to display a popup
    private void makeToast(String popup) {
        Toast.makeText(this, popup, Toast.LENGTH_SHORT).show();
    }

}

Here's the xml-document:

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

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="basicnote_settings"
    android:title="@string/settings_label">

    <PreferenceCategory android:title="Text size">
        <ListPreference android:key="text_size_list"
        android:title="Widget text size"
        android:summary="@string/text_size_summary"
        android:entries="@array/text_size_options"
        android:entryValues="@array/text_size_values"
        android:defaultValue="small" />
    </PreferenceCategory>

    <PreferenceCategory android:title="Other settings">
        <CheckBoxPreference android:key="line_counter_cbox"
            android:title="Line counter"
            android:summary="@string/hidden_lines_summary"
            android:defaultValue="true" />
    </PreferenceCategory>

</PreferenceScreen>

And here are my resource strings:

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

<!-- Defines various resources -->
<resources>
    <string name="app_name">basicNote</string>
    <string name="note_hint">Tap to add some notes ...</string>
    <string name="fake_load">Loading notes ...</string>

    <string name="settings_label">basicNote settings</string>

    <string name="text_size_summary">Set the size of the widget text</string>
    <string-array name="text_size_options">
        <item>Small (12sp)</item>
        <item>Medium (13sp)</item>
        <item>Large (15sp)</item>
    </string-array>
    <string-array name="text_size_values">
        <item>small</item>
        <item>medium</item>
        <item>large</item>
    </string-array>

    <string name="hidden_lines_summary">Show/hide the number of lines not shown in the widget</string>
</resources>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

待"谢繁草 2024-12-25 09:51:05

您在 String 对象上使用 == 。使用 .equals(yourString) 代替。

Your using == on a String object. Use .equals(yourString) instead.

极致的悲 2024-12-25 09:51:05

尝试将字符串中的所有 == 与 equals() 进行比较。例如:

if(widgetTextSize.equals("small"))
{
}

看看这篇文章

try to change all == in string compare to equals(). For example:

if(widgetTextSize.equals("small"))
{
}

and take a look at this article

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文