2013-02-26 72 views
0

將用戶定義的首選項保存在另一個首選項(以逗號分隔)還是存在更清潔更好的方法可以嗎?如何保存用戶定義的首選項?

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

回答

0

參考SharedPreferences.Editor。如果你有問題,請告訴我。

編輯:對不起,您粘貼的代碼第一次沒有加載。我將在這裏介紹一種方法,從jiffy中保存文件中的偏好。

在這裏,他們是,對不起,如果有一些錯誤,我在記事本中鍵入它。

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

+0

這將節省一個優先選擇,並把它默認值爲0(這不是索引)。我需要保存多個信息... – dtrunk 2013-02-26 13:03:59