gpt4 book ai didi

android - 无法使用 PreferenceActivity 设置首选项

转载 作者:太空宇宙 更新时间:2023-11-03 12:24:10 26 4
gpt4 key购买 nike

我有这个主要 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);
}
// ...
}

还有这个 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”,这意味着此首选项不存在或我命名错误。

知道我做错了什么吗?

问候,

最佳答案

这可能会帮助您回到正轨并拥有工作偏好:

XML 文件:首选项.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
}
}

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

关于android - 无法使用 PreferenceActivity 设置首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6777138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com