사용자 정의 된 환경 설정을 다른 환경 설정 (쉼표로 구분)에 저장해도 좋고 깨끗합니까? 더 좋은 방법이 있습니까?사용자 정의 기본 설정을 저장하는 방법은 무엇입니까?
<?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입니다.
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_.:/]+$"));
}
}
그것은 다음과 같이해야하며, 편집 및 삭제 항목에 의해 확장됩니다 :
이 ONE 환경 설정을 저장하고 그것을 얻을 것 디폴트 치는 0 (인덱스는 아니다). 여러 infos 저장해야합니다 ... – dtrunk