无法使用 PreferenceActivity 获取首选项设置

发布于 2024-11-25 17:31:56 字数 1402 浏览 1 评论 0原文

我有这个主要活动:

public class Home extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        initializeStyle();
    }
    private void initializeStyle() {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String foobar = prefs.getString("foobar", "hi");
        Log.i("MyActivity", foobar);
    }
// ...
}

和这个 PreferenceActivity:

public class Preferences extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

使用这个 XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory 
        android:key="preferences_font"
        android:title="@string/preferences_font">
    <EditTextPreference
            android:title="@string/preferences_font_size"
            android:key="foobar"
            android:defaultValue="12">
    </EditTextPreference>
</PreferenceCategory>
</PreferenceScreen>

Log.i 打印“hi”,所以这意味着这个首选项不存在或者我命名得很糟糕。

你知道我做错了什么吗?

问候,

I have this main activity:

public class Home extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        initializeStyle();
    }
    private void initializeStyle() {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String foobar = prefs.getString("foobar", "hi");
        Log.i("MyActivity", foobar);
    }
// ...
}

And this PreferenceActivity:

public class Preferences extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

With this XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory 
        android:key="preferences_font"
        android:title="@string/preferences_font">
    <EditTextPreference
            android:title="@string/preferences_font_size"
            android:key="foobar"
            android:defaultValue="12">
    </EditTextPreference>
</PreferenceCategory>
</PreferenceScreen>

Log.i prints "hi", so it means this preference doesn't exist or I named it badly.

Any idea about what I've done wrong?

Regards,

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

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

发布评论

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

评论(1

梦回梦里 2024-12-02 17:31:56

这可能会帮助您回到正轨并获得工作首选项:

XML 文件:preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="Preferences Font">
        <EditTextPreference 
            android:title="Select Font Size" 
            android:key="foobar" 
            android:summary="Enter Font Size"
            android:defaultValue="12" />
    </PreferenceCategory>
</PreferenceScreen> 

JAVA 文件 1:Home.java

public class Home extends Activity {

    public SharedPreferences prefs;

    static public String fontPref; //you can make it an int or whatever you need
    static public String myValue; //to store new pref value

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

         setContentView(R.layout.home);
         initializeStyle();
    } 

    private void initializeStyle() {
       prefs = PreferenceManager.getDefaultSharedPreferences(this);
       fontPref = prefs.getString("foobar", "hi"); //this doesn't actually change the value
       myValue = "This is a test";
       Log.i("MyActivity", foobar); //won't show change yet
    }
}

JAVA 文件 2:Prefernce.java

import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
//... and any other imports

public class Preferences extends PreferenceActivity  implements OnSharedPreferenceChangeListener{

    private SharedPreferences prefs;

    @Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        //add other stuff here if you need, especially if you might have some
        //prefs that use buttons and need to set up your onClickListener for one
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPref, String key) {
        Editor editor = sharedPref.edit();
        if(key.equalsIgnoreCase("foobar")){
            Home.fontPref = sharedPref.getString("foobar", "some value");
            editor.putString("foobar", Home.myValue); //where ever or whatever new value is 
        }

        editor.commit();
        Log.i("PrefsActivity", foobar); //should show change now
    }
} 

希望这可以帮助您走上正确的轨道。当然,有很多方法可以做到这些,祝你好运,希望它能有所帮助。

This might help you get back on track and having working preferences:

XML FILE: preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="Preferences Font">
        <EditTextPreference 
            android:title="Select Font Size" 
            android:key="foobar" 
            android:summary="Enter Font Size"
            android:defaultValue="12" />
    </PreferenceCategory>
</PreferenceScreen> 

JAVA FILE 1: Home.java

public class Home extends Activity {

    public SharedPreferences prefs;

    static public String fontPref; //you can make it an int or whatever you need
    static public String myValue; //to store new pref value

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

         setContentView(R.layout.home);
         initializeStyle();
    } 

    private void initializeStyle() {
       prefs = PreferenceManager.getDefaultSharedPreferences(this);
       fontPref = prefs.getString("foobar", "hi"); //this doesn't actually change the value
       myValue = "This is a test";
       Log.i("MyActivity", foobar); //won't show change yet
    }
}

JAVA FILE 2: Prefernce.java

import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
//... and any other imports

public class Preferences extends PreferenceActivity  implements OnSharedPreferenceChangeListener{

    private SharedPreferences prefs;

    @Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        //add other stuff here if you need, especially if you might have some
        //prefs that use buttons and need to set up your onClickListener for one
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPref, String key) {
        Editor editor = sharedPref.edit();
        if(key.equalsIgnoreCase("foobar")){
            Home.fontPref = sharedPref.getString("foobar", "some value");
            editor.putString("foobar", Home.myValue); //where ever or whatever new value is 
        }

        editor.commit();
        Log.i("PrefsActivity", foobar); //should show change now
    }
} 

Hopefully this may help you get on the right track. Of course there are many ways to do these things so, good luck hope it helps.

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