如何保存用户定义的偏好?

时间:2013-02-26 12:40:04

标签: android android-preferences

将用户定义的首选项保存在另一个首选项(以逗号分隔)中是否可以,或者是否有更清洁,更好的方法?

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory android:key="client" android:title="@string/client">

        <PreferenceScreen
            android:key="manage_sources"
            android:title="@string/manage_sources"
            android:persistent="false">

            <com.example.preference.EditSourcePreference
                android:key="add_source"
                android:title="@string/add_source"
                android:dialogTitle="@string/add_source" />

            <PreferenceCategory
                android:key="sources"
                android:title="@string/sources" />

        </PreferenceScreen>

    </PreferenceCategory>

</PreferenceScreen>

这是一个嵌套的PreferenceScreen,提供添加源的首选项。 这是Java代码:

public class EditSourcePreference extends EditTextPreference implements TextWatcher {

    private EditText source;
    private Button positiveButton;

    private String originalSource;

    public EditSourcePreference(Context context) {
        super(context);
    }

    public EditSourcePreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void showDialog(Bundle state) {
        super.showDialog(state);

        source = getEditText();
        source.addTextChangedListener(this);

        positiveButton = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
        afterTextChanged(source.getText());

        originalSource = source.getText().toString();
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (!positiveResult) {
            return;
        }

        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getContext());
        SharedPreferences.Editor editor = pref.edit();

        String sources = pref.getString("sources", ""),
                newSource = source.getText().toString();

        boolean append = sources.indexOf(originalSource) != -1; // TODO: needs debugging!!!

        if (append) {
            if (!"".equals(sources)) {
                sources += ";";
            }

            sources += newSource;
        } else {
            sources = sources.replace(originalSource, newSource);
        }

        editor.putString("sources", sources);
        editor.commit();

        super.onDialogClosed(positiveResult);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        positiveButton.setEnabled(s.toString().matches("^[a-z0-9_:/]+[.]+[a-z0-9_.:/]+$"));
    }

}

它应如下所示,并将通过编辑和删除条目进行扩展: How it looks like

2 个答案:

答案 0 :(得分:0)

请参阅SharedPreferences.Editor。如果您有任何疑问,请与我联系。

编辑:抱歉,不知何故,您粘贴的代码第一次没有加载。我将在这里提供一种方法,从快速保存的文件中获取首选项。

在这里,对不起,如果有错误,我在记事本中输入。

private final String PREFS_NAME = "prefs";

private final String KEY_AGE = "age";
private final String KEY_COUNTRY = "location";

  SharedPreferences sharedPreferences = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
  SharedPreferences.Editor editor = sharedPreferences.edit();

  editor.putInt(KEY_AGE, "13");
  editor.putString(KEY_COUNTRY, "USA");

  editor.commit();

// To retrieve the values

  SharedPreferences sharedPreferences = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
  sharedPreferences.getInt(KEY_AGE, 0);
  sharedPreferences.getString(KEY_COUNTRY, 0);

如果这不符合您的需求,请使用database

答案 1 :(得分:0)