如果您面临来自以下方法的弃用警告:
addPreferencesFromResource(R.xml.default_values);
您必须使用 PreferenceFragment 因为 API 11 及更高版本需要这种方法。
以下是如何使用 PreferenceFragment 的示例:
1.创建/res/xml/preferences.xml来定义我们的偏好。
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="PreferenceCategory A">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="title_checkbox_preference"
android:summary="summary_checkbox_preference" />
</PreferenceCategory>
<PreferenceCategory
android:title="PreferenceCategory B">
<EditTextPreference
android:key="edittext_preference"
android:title="title_edittext_preference"
android:summary="summary_edittext_preference"
android:dialogTitle="dialog_title_edittext_preference" />
</PreferenceCategory>
2.创建 PrefsFragment.java 将 PreferenceFragment 扩展为 addPreferencesFromResource。
package com.example.androidpreferencefragment;
import android.os.Bundle;
import android.preference.PreferenceFragment;
public class PrefsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
3.创建SetPreferenceActivity.java来加载PrefsFragment。
package com.example.androidpreferencefragment;
import android.app.Activity;
import android.os.Bundle;
public class SetPreferenceActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new PrefsFragment()).commit();
}
}
4.修改主布局,/res/layout/activity_main.xml,以显示首选项。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<CheckBox
android:id="@+id/prefCheckBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="CheckBoxPreference" />
<TextView
android:id="@+id/prefEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
5.主Activity,MainActivity.java。
package com.example.androidpreferencefragment;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.TextView;
public class MainActivity extends Activity {
CheckBox prefCheckBox;
TextView prefEditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefCheckBox = (CheckBox)findViewById(R.id.prefCheckBox);
prefEditText = (TextView)findViewById(R.id.prefEditText);
loadPref();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
/*
* Because it's onlt ONE option in the menu.
* In order to make it simple, We always start SetPreferenceActivity
* without checking.
*/
Intent intent = new Intent();
intent.setClass(MainActivity.this, SetPreferenceActivity.class);
startActivityForResult(intent, 0);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
//super.onActivityResult(requestCode, resultCode, data);
/*
* To make it simple, always re-load Preference setting.
*/
loadPref();
}
private void loadPref(){
SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean my_checkbox_preference = mySharedPreferences.getBoolean("checkbox_preference", false);
prefCheckBox.setChecked(my_checkbox_preference);
String my_edittext_preference = mySharedPreferences.getString("edittext_preference", "");
prefEditText.setText(my_edittext_preference);
}
}
5.最后修改AndroidManifest.xml,添加SetPreferenceActivity。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidpreferencefragment"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SetPreferenceActivity"
android:label="@string/title_activity_main" >
</activity>
</application>