如何使用ListPreferences保存和应用用户设置?

时间:2013-10-23 06:53:26

标签: java android settings sharedpreferences listpreference

我实际上要做的是在用户从我的ListPreference中选择适当的选项后更改布局和文本背景颜色

public class PrefFragment extends PreferenceFragment implements OnPreferenceChangeListener {

private static final String PREF_THEME="themeset";
ListPreference theme_selector;
  @Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
       addPreferencesFromResource(R.xml.preferences);

       theme_selector = (ListPreference) findPreference(PREF_THEME); 
       theme_selector.setOnPreferenceChangeListener(this);      
        };
    public boolean onPreferenceChange(Preference preference, Object newValue) {
               return true;
             }
        }

这是我的Prefernce Fragment

这是我的偏好.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<ListPreference
        android:key="themeset"
        android:entries="@array/themeselection"
        android:summary="@string/pref_theme_summary" 
        android:entryValues="@array/themeValues"
        android:title="@string/pref_themes" 
        android:defaultValue="1"/>

这是我的array.xml

<resources>
   <string-array name="themeselection">
        <item name="Black">Black</item>
        <item name="White7">White</item>
   </string-array> 
   <string-array name="themeValues">
        <item name="Black">1</item>
        <item name="White">2</item>
   </string-array>
</resources>

以下是我的主要活动

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity implements OnSharedPreferenceChangeListener {

    private int theme_setting;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout theme =(RelativeLayout) findViewById(R.id.layout);
        TextView txtcolor = (TextView) findViewById(R.id.txt);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.registerOnSharedPreferenceChangeListener(this);

        theme_setting = Integer.parseInt((prefs.getString("themeset", "1")));

        if (theme_setting == 1)
        {
            theme.setBackgroundColor(Color.parseColor("#000000"));
            txtcolor.setTextColor(Color.parseColor("#FFFFFF"));
            this.recreate();
        }
          else
          {
            theme.setBackgroundColor(Color.parseColor("#FFFFFF"));
            txtcolor.setTextColor(Color.parseColor("#000000"));
            this.recreate();  
          }
    }
    @Override
    public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
        if (arg1.equals("themeValues")) 
        {
            theme_setting = Integer.parseInt((arg0.getString("themeset", "1")));
        }
    }

    @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.action_settings:
            Intent i = new Intent(this, PrefSummary.class);
            startActivityForResult(i,1);
            break;
        }
        return true;
        }
        }

正如我之前所说,我想做的只是通过以编程方式设置/更改布局背景颜色和字体颜色来更改主题,对于上面的代码,我得到一个冻结的白色应用程序屏幕,好像布局不是'加载。我意识到我主要在主要活动中做了一些可怕的错误,我希望能帮助你实现这个目标。

1 个答案:

答案 0 :(得分:2)

我意识到&#34; this.recreate&#34;代码导致无限循环,因此我写了很多垃圾代码,认为这是偏好设置的错误,下面我将展示一些更清洁和工作的代码:

修改的偏好片段:

public class PrefFragment extends PreferenceFragment {
public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
           addPreferencesFromResource(R.xml.preferences);};}

在主要活动中

1.删除或评论&#34; this.recreate()&#34;在if ... else。

2.Modified

public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
        if (arg1.equals("themeValues")) 
        {
            theme_setting = Integer.parseInt((arg0.getString("themeset", "1")));
        }

public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
        this.recreate();//So the recreate function will be called only once and only when a change is detected.
    }

最后这里的答案不是针对问题(因为初学者没有问题),罪魁祸首是recreate()函数,而不是使用SharedPreferences或ListPreferences。