【问题标题】:How to save and apply user settings using ListPreferences?如何使用 ListPreferences 保存和应用用户设置?
【发布时间】:2013-11-01 07:43:36
【问题描述】:

一旦用户从我的 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;
             }
        }

那是我的首选片段

这是我的preferences.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;
        }
        }

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

【问题讨论】:

  • 我修好了!!稍后会发布我的发现,因为我的声誉太少。

标签: java android settings sharedpreferences listpreference


【解决方案1】:

我意识到“this.recreate”代码导致了无限循环,因此我写了很多垃圾代码,认为这是首选项设置的问题,下面我将展示更简洁和有效的代码代码:

修改偏好片段:

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

在主要活动中

1.在 if...else 中删除或注释了“this.recreate()”。

2.修改

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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 2016-03-06
    • 1970-01-01
    相关资源
    最近更新 更多