处理SharedPreferences更改的更好方法

时间:2013-07-19 17:17:02

标签: android

我有一个项目有两个活动(MainActivity和CustomPreferenceActivity)。我有一些代码,当单击一个按钮时,它会启动CustomPreferenceActivity。根据CustomPreferenceActivity上的选择,TextView的字体颜色将发生变化。但是,我不喜欢我的问题解决方案(绑定onResume来更新UI)。还有其他建议吗?提前谢谢。

MainActivity

public class MainActivity extends Activity {

private int colorResId;
private int fontSize;
private OnClickListener buttonListener = getButtonListener();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button)findViewById(R.id.preferenceButton);
    button.setOnClickListener(buttonListener);


}

@Override
public void onResume()
{
    super.onResume();
    updateUiBasedOnPreferences();
}

private void updateUiBasedOnPreferences()
{
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
    String colorPreference = pref.getString("colorPreference", "default");

    colorResId = convertStringToColorResId(colorPreference);
    fontSize = pref.getInt("fontSize", 24);

    TextView textView = (TextView)findViewById(R.id.textView);
    textView.setTextColor(colorResId);
    textView.setTextSize(fontSize);

}
private int convertStringToColorResId(String input)
{
    if (input.equals("GREEN"))
        return Color.GREEN;
    else if (input.equals("RED"))
        return Color.RED;
    else if (input.equals("BLUE"))
        return Color.BLUE;
    else
        return Color.BLACK;
}

private OnClickListener getButtonListener()
{
    return new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), CustomPreferencesActivity.class);
            startActivity(intent);
        }
    };
}

CustomPreferencesActivity

public class CustomPreferencesActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener 
{

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.preferences);
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    pref.registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
    Preference pref = findPreference(key);
    pref.setSummary(sharedPreferences.getString(key, ""));
}
}

2 个答案:

答案 0 :(得分:0)

您可以实现一个单例类和一个侦听器接口来实现此技巧。

  1. OnPreferenceChangeListener,MainActivity实现此接口以更新UI

  2. OnPreferenceChangeHeler,持有OnPreferenceChangeListener(即MainActivity)的引用

  3. 更新时的OnPreferenceChangeHeler的CustomPreferencesActivity调用方法

答案 1 :(得分:0)

您可以使用Intent而不是仅在主活动中使用startActivityForResult(Intent, int),将您的偏好设置活动传递回包含颜色信息的startActivity(Intent)回到您的主要活动。然后可以在onActivityResult(int, int, Intent)期间读取意图数据,在首选项活动完成时自动调用该数据。代码如下所示:

public class MainActivity extends Activity {

    // ...

    public static final int TEXT_COLOR_REQUEST_CODE = 1;

    private OnClickListener getButtonListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), CustomPreferencesActivity.class);
                startActivityForResult (intent, TEXT_COLOR_REQUEST_CODE);
            }
        };
    }

    @Override
    protected void onActivityResult (int requestCode, int resultCode, Intent data) {
        if ((requestCode == TEXT_COLOR_REQUEST_CODE) && (resultCode == RESULT_OK)) {
            // Extract color information from data and update UI.
            updateUI(data);
        }
    }
}

当您的偏好活动中的颜色发生变化时,您可以添加以下代码:

Intent intent = new Intent();
intent.putExtra("key", colorData);
setResult(RESULT_OK, colorData);