我想在我的首选项屏幕底部添加几个按钮,用于设置默认值和恢复默认值。这个答案不包括如何使用 PreferenceFragment 做到这一点。完成此操作的推荐方法是什么。
加载首选项片段的活动:
public class SettingsActivity extends Activity {
@Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate( savedInstanceState);
// load up the preferences fragment
getFragmentManager().beginTransaction().replace( android.R.id.content, new PrefsSettingsFragment()).commit();
}
}
PreferenceFragment 实现:
public class PrefsSettingsFragment extends PreferenceFragment {
@Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate( savedInstanceState);
addPreferencesFromResource( R.xml.preferences);
}
}
首选项.xml:
<?xml version="1.0"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference android:key="edit_text_preference_server_address" android:title="@string/preference_server_address"/>
<SwitchPreference android:key="switch_preference_bat" android:title="@string/preference_bat"/>
<SwitchPreference android:key="switch_preference_comm" android:title="@string/preference_comm"/>
<SwitchPreference android:key="switch_preference_dev_mode" android:title="@string/preference_dev_mode" android:defaultValue="true"/>
</PreferenceScreen>