public class MainActivity extends ActionBarActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
StringBuilder str = new StringBuilder();
str.append("\nkey1:"+sharedPrefs.getString("key1", "NULL"));
str.append("\nkey2:"+sharedPrefs.getBoolean("key2", false));
str.append("\nkey3:"+sharedPrefs.getString("key3", "NULL"));
TextView settingsTextView = (TextView) findViewById(R.id.txt_setting);
settingsTextView.setText(str.toString());
}
@Override public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
@Override public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_settings) {
Intent intent = new Intent(MainActivity.this, UserSettingActivity.class);
MainActivity.this.startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout 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">
<TextView android:id="@+id/txt_setting"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
2.UserSettingActivity.java
public class UserSettingActivity extends PreferenceActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
/res/xml/settings.xml
Resource files
/res/values/arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="user_pref" >
<EditTextPreference
android:title="key1_title"
android:key="key1"
android:summary="key1_summary"/>
</PreferenceCategory>
<PreferenceCategory android:title="update_setting" >
<CheckBoxPreference
android:title="key2_title"
android:key="key2"
android:defaultValue="true"
android:summary="key2_summary" >
</CheckBoxPreference>
<ListPreference
android:title="key3_title"
android:key="key3"
android:entries="@array/key3_entries"
android:entryValues="@array/key3_values"
android:summary="key3_summary" />
</PreferenceCategory>
</PreferenceScreen>
Resource files
/res/values/arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="key3_entries">
<item name="1">one</item>
<item name="2">two</item>
<item name="3">three</item>
</string-array>
<string-array name="key3_values">
<item name="1">one</item>
<item name="2">two</item>
<item name="3">three</item>
</string-array>
</resources>
/res/menu/settings.xml
<item android:id="@+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/menu_settings"
android:icon="@android:drawable/ic_menu_preferences"/>
Output file : /data/data/package_name/pref/pref.xml
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="key1">ak1</string>
<string name="key2">7</string>
<boolean name="key3" value="true" />
</map>
ไม่มีความคิดเห็น:
แสดงความคิดเห็น