所以我正在了解 SharedPreferences在 Android 中。
我想我会很快尝试模拟一些简单的东西。当用户从选项列表中选择一个特定选项时(在本例中为 3 个选项)- 布局文件将相应更改。 However, it always assumes the default value supplied, hence never changes the layout when another option is selected - so when another option is selected, it is recorded fine, but the layout doesn't change, this leads me to believe that there is perhaps首先获得偏好时出现问题,我想知道是否有人可以快速查看并发现一些明显的东西,或者根据他们的经验/智慧发现一些不那么明显的东西 - 这是代码:
主要 Activity .java:
package com.example.preferenceslayout;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity
implements OnSharedPreferenceChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadPreferences();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.preferences:
startActivity(new Intent(getApplicationContext(),
AppPreferences.class));
break;
}
return false;
}
public void loadPreferences() {
SharedPreferences prefs = getApplicationContext().
getSharedPreferences("mode", MODE_PRIVATE);
int layoutPref = prefs.getInt("mode", 10);
switch(layoutPref) {
case 10:
setContentView(R.layout.activity_main);
break;
case 20:
setContentView(R.layout.second_layout);
break;
case 30:
setContentView(R.layout.third_layout);
break;
}
prefs.registerOnSharedPreferenceChangeListener(MainActivity.this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
loadPreferences();
}
}
首选项.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<ListPreference
android:key="mode"
android:title="@string/mode"
android:defaultValue="1"
android:summary="@string/summary"
android:entries="@array/listArray"
android:entryValues="@array/listValues" />
</PreferenceScreen>
AppPreferences.xml:
package com.example.preferenceslayout;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class AppPreferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
}
}
主.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
<item
android:id="@+id/preferences"
android:title="Preferences" />
</menu>
PreferencesLayoutManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.preferenceslayout"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.preferenceslayout.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.preferenceslayout.AppPreferences">
<intent-filter>
<action android:name=".AppPreferences" />
</intent-filter>
</activity>
</application>
</manifest>
数组.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="listArray">
<item>10</item>
<item>20</item>
<item>30</item>
</string-array>
<string-array name="listValues">
<item>1</item>
<item>2</item>
</string-array>
</resources>
最佳答案
您永远不会写您的喜好。你需要一个 prefs.putInt("mode", some_int);
某处。此外,您的听众毫无用处 - 放弃它。
您正在写入默认共享首选项,因为首选项屏幕在那里写入。 “模式”只是关键——默认共享首选项中有一个键,你的首选项文件(名为“mode_something.xml”)中有一个键——但是你写了一个(自动通过首选项屏幕),你读了另一个(永远不会设置)
关于Android SharedPreferences 没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21713087/