代码之家  ›  专栏  ›  技术社区  ›  dtrunk

如何保存用户定义的首选项?

  •  0
  • dtrunk  · 技术社区  · 11 年前

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

    <?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>
    

    这是一个嵌套的首选项屏幕,提供用于添加源的首选项。 以下是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 回复  |  直到 11 年前
        1
  •  0
  •   Gareth Davis    11 年前

    提到 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 .