首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓SharedPreferences不变

安卓SharedPreferences不变
EN

Stack Overflow用户
提问于 2014-02-11 21:10:39
回答 3查看 1.1K关注 0票数 1

因此,我正在安卓系统中学习SharedPreferences

我想我会很快地试着模仿一些简单的东西。当用户从选项列表(在本例中为3个选项)中选择特定选项时,布局文件将相应更改。然而,它总是假定所提供的默认值,因此在选择另一个选项时不会更改布局--因此,当选择另一个选项时,它会被记录得很好,但布局不会改变,这使我相信,在获得首选项时可能会出现问题,我想知道是否有人可以快速查看并发现一些显而易见的东西,或者用他们的经验/智慧发现一些不明显的东西--下面是代码:

MainActivity.java:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-02-12 00:25:34

你从不写信给你的喜好。你需要一个prefs.putInt("mode", some_int);。另外,你的听众也没有任何用处--放下它。

您正在写入默认的共享首选项,原因是首选项屏幕在那里写入。“模式”只是键--默认共享首选项中有一个键,您的首选项文件中有一个键(名为"mode_something.xml") --但是您编写了一个键(通过首选项屏幕自动),然后读取另一个(从未设置)。

票数 2
EN

Stack Overflow用户

发布于 2014-02-11 21:55:16

我并没有深入了解您的代码,但是在进行了快速的概述之后,以下内容引起了我的注意:

您要在ListPreference中的preference.xml中设置这个

代码语言:javascript
复制
android:entries="@array/listArray"
android:entryValues="@array/listValues" 

但是在array.xml中,条目字符串数组中有3项,而listValues字符串数组中只有2项。

代码语言:javascript
复制
<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>

您应该将第三项添加到listValues字符串数组中。

顺便说一句:您应该考虑重命名"listArray“,以便更清楚地知道它包含了列表中的条目。所以也许叫它"listEntries“。(记住也要更改对android:entries="@array/listEntries"的引用)一般来说,条目是用户实际看到的,这些值就是android所使用的。因此,例如,字符串数组可能如下所示:

代码语言:javascript
复制
<string-array name="listEntries">
    <item>one apple</item>
    <item>two apples</item>
    <item>three apples</item>
</string-array>
<string-array name="listValues">
    <item>1</item>
    <item>2</item>
    <item>3</item>
</string-array>
票数 1
EN

Stack Overflow用户

发布于 2014-02-11 22:16:51

每次更改首选项时都会调用onSharedPreferenceChanged,所以不要每次更改首选项时实例化一个新的SharedPreferences对象,您必须做的是:

首先,让SharedPreferences成为一个类属性,这样您就可以在任何地方访问活动中的首选项:

代码语言:javascript
复制
private SharedPreferences prefs;

现在,在onCreate()调用中:

代码语言:javascript
复制
prefs=PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);

最后,您的loadPreferences()方法应该如下所示:

代码语言:javascript
复制
public void loadPreferences() {

    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;
    }

}

希望它能帮到你!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21713087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档