【问题标题】:Android XML Preference issue. Can't make it persistentAndroid XML 首选项问题。不能让它持久
【发布时间】:2012-06-28 14:44:44
【问题描述】:

我有一个非常简单的活动来显示偏好片段:

public class PreferencesActivity extends Activity {

Fragment frag = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    if (frag == null) {
        // If not, instantiate and add it to the activity
        frag = new PrefsFragment();
        ft.add(android.R.id.content, frag, frag.getClass().getName());
    } else {
        // If it exists, simply attach it in order to show it
        ft.attach(frag);
    }
    ft.commit();
}

private static class PrefsFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }

}
}

和preferences.xml 持久为真:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:enabled="true"
    android:persistent="true"
    android:title="@string/settings" >
    <EditTextPreference
        android:dialogTitle="@string/dialog_ip"
        android:negativeButtonText="@android:string/cancel"
        android:persistent="true"
        android:positiveButtonText="@android:string/ok"
        android:title="@string/ip" />
</PreferenceScreen>

如果我打开 EditTextPreference,写一些东西,关闭对话框并再次打开它。价值还在。但就是这样......如果我单击“后退”按钮,然后在首选项屏幕上再次输入,我已经丢失了所写的内容。如果退出应用程序也不保存。

我错过了什么吗?

运行时间: 安卓 4.0.3 华硕TF300

【问题讨论】:

    标签: android xml persistent preference


    【解决方案1】:

    您的EditTextPreference 没有key 值。例如:

           <EditTextPreference
                android:key="edittext_preference"
                android:title="@string/title_edittext_preference"
                android:summary="@string/summary_edittext_preference"
                android:dialogTitle="@string/dialog_title_edittext_preference" />
    

    【讨论】:

    • ooohhh... 所以如果没有密钥,首选项就不能工作?我认为关键只是从代码中访问首选项。我会尝试并报告。
    猜你喜欢
    • 1970-01-01
    • 2013-05-25
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    相关资源
    最近更新 更多