Android的共享preferences不改变 [英] Android SharedPreferences not changing
问题描述
所以,我正在学习有关共享preferences 一>中的Android。
我想我很快就会试着小样简单的东西。当用户选择从选项(在这种情况下,3个选择)列表的特定选项 - 布局文件将相应地改变。然而,它总是假定提供的默认值,因此不会改变当另一个选项被选中的布局 - 因此选择了其他选项时,它被记录罚款,但布局不改变,这让我相信,有可能是摆在首位获得preferences时发生的问题,我想知道,如果有人可以快速浏览一下和点东西很明显,或者不那么明显与他们的经验/智慧 - 这里是code:
MainActivity.java:
包com。示例preferenceslayout。进口android.app.Activity;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.content.Shared preferences.OnShared preferenceChangeListener;
进口android.os.Bundle;
。进口的Android preference preferenceManager。
进口android.view.Menu;
进口android.view.MenuItem;公共类MainActivity扩展活动
实现OnShared $ P $ {pferenceChangeListener @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
负荷preferences();
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
} 公共布尔onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id. preferences:
startActivity(新意图(getApplicationContext()
应用preferences.class));
打破;
}
返回false;
} 公共无效负载preferences(){
共享preferences preFS = getApplicationContext()。
getShared preferences(模式,MODE_PRIVATE);
INT布局preF = prefs.getInt(模式,10);
开关(布局preF){
案例10:
的setContentView(R.layout.activity_main);
打破;
案例20:
的setContentView(R.layout.second_layout);
打破;
案例30:
的setContentView(R.layout.third_layout);
打破;
}
prefs.registerOnShared preferenceChangeListener(MainActivity.this);
} @覆盖
公共无效onShared preferenceChanged(共享preferences为arg0,ARG1字符串){
负荷preferences();
}
}
preference.xml:
<?XML版本=1.0编码=UTF-8&GT?;
< preferenceScreen的xmlns:机器人=http://schemas.android.com/apk/res/android>
<名单preference
机器人:键=模式
机器人:标题=@字符串/模式
机器人:设置defaultValue =1
机器人:总结=@字符串/摘要
机器人:项=@阵列/ listArray
机器人:entryValues =@阵列/ listValues/>
< / preferenceScreen>
应用preferences.xml:
包com。示例preferenceslayout。进口android.os.Bundle;
。进口的Android preference preferenceActivity;公共类应用preferences扩展$ P $ {pferenceActivity @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
加preferencesFromResource(R.xml preference);
}
}
main.xml中:
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目
机器人:ID =@ + ID / action_settings
机器人:orderInCategory =100
机器人:showAsAction =从不
机器人:标题=@字符串/ action_settings/>
<项目
机器人:ID =@ + ID / preferences
机器人:标题=preferences/>
&所述; /菜单>
preferencesLayoutManifest.xml
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com。示例。preferenceslayout
安卓版code =1
机器人:=的versionName1.0> <用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =18/> <应用
机器人:allowBackup =真
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名字=。com。示例preferenceslayout.MainActivity
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动机器人:名字=。com。示例preferenceslayout.App preferences>
&所述;意图滤光器>
<作用机器人:应用程序preferencesNAME = />
&所述; /意图滤光器>
< /活性GT;
< /用途>
< /清单>
array.xml
<?XML版本=1.0编码=UTF-8&GT?;
<资源>
<字符串数组名=listArray>
<项目大于10< /项目>
<项目> 20℃/项目>
<项目> 30℃/项目>
< /字符串数组>
<字符串数组名=listValues>
<项目> 1 LT; /项目>
<项目> 2'; /项目>
< /字符串数组>
< /资源>
您永远写的您preferences。你需要一个 prefs.putInt(模式,some_int);
的某个地方。此外,您的听众任何无济于事 - 砸
您正在写入的默认共享preferences 的原因preferences屏幕里面写。 模式仅仅是个关键 - 有默认的共享preferences一个密钥,一个在你的preference文件(名为mode_something.xml) - 但你写一个(通过$ P自动$ pferences屏),并且阅读了其他的(这是从未设置)
So I am learning about SharedPreferences within Android.
I thought I would quickly try and mock up something simple. When the user selects a particular option from a list of options (3 options in this case) - the layout file will change accordingly. 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 a problem occurring when getting the preferences in the first place, I was wondering if someone could take a quick look and spot something obvious, or not so obvious with their experience/wisdom - here is the code:
MainActivity.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();
}
}
preference.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);
}
}
main.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>
array.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>
You never write to your preferences. You need a prefs.putInt("mode", some_int);
somewhere. Also your listener serves no purpose whatsoever - drop it.
You are writing to the default shared preferences cause preferences screens write there. The "mode" is just the key - there is one key in the default shared preferences and one in your preference file (named "mode_something.xml") - but you write the one (automatically via the preferences screen) and you read the other (which is never set)
这篇关于Android的共享preferences不改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!