我問:「有人知道一個簡單的(或兩個複選框)modern fragment-based PreferenceActivity
?或任何保存偏好但不被棄用的好教程嗎?」
我昨晚晚上9點才做。 * EDITED 5/16/15 *
這是一個完整的工作首選項活動,儘管它沒有所有可繪製,mipmap,維度和值文件夾和文件。我從Sams的建議中下載了它自學 24小時Android應用開發並對其進行了修改。強調我的。
![enter image description here](https://i.stack.imgur.com/R1zTB.png)
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.whatever.prefs"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.whatever.prefs.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.whatever.prefs.SettingsActivity"
android:label="@string/title_activity_settings" >
</activity>
</application>
</manifest>
MainActivity.java:
package com.whatever.prefs;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final String SETTINGS = "com.whatever.prefs.settings";
public static final String FIRST_USE = "com.whatever.prefs.firstUse";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* BAD IDEA--CREATES NEW SET OF PREFS
SharedPreferences preferences = getSharedPreferences(SETTINGS, MODE_PRIVATE);
Do this v v v v v v v v v v v instead! */
preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean firstUse = preferences.getBoolean(FIRST_USE, true);
if (firstUse){
Toast helloMessage = Toast.makeText(getApplicationContext(), "Hello First Time
User",Toast.LENGTH_LONG);
helloMessage.show();
Editor editor = preferences.edit();
editor.putBoolean(FIRST_USE, false);
editor.commit();
}
else {
Toast.makeText(getApplicationContext(), "Second+ use", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
SettingsFragment.java:
package com.whatever.prefs;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.util.Log;
public class SettingsFragment extends PreferenceFragment
implements OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().
registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().
unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences(getActivity());
if (key.equalsIgnoreCase("pie_type")){
Log.w("Settings", sharedPref.getString(key, ""));
}
}
}
重小號\菜單\ activity_main.xml中:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/menu_settings"
android:orderInCategory="100"
app:showAsAction="ifRoom"
android:title="@string/menu_settings"/>
</menu>
資源\佈局\ 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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Hi world" />
</RelativeLayout>
資源\佈局\ activity_settings.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">
<fragment android:name="com.whatever.prefs.SettingsFragment"
android:id="@+id/settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
字符串。XML:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Hour11App</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_settings">Settings</string>
<string-array name="pie_array">
<item>apple</item>
<item>blueberry</item>
<item>cherry</item>
<item>coconut cream</item>
</string-array>
</resources>
styles.xml:
<resources>
<!-- Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices. -->
<style name="AppBaseTheme" parent="android:Holo.ButtonBar">
<!-- Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
SettingsActivity.java:
package com.whatever.prefs;
import android.app.Activity;
import android.os.Bundle;
public class SettingsActivity extends Activity {
public static final String SETTINGS = "com.whatever.prefs.settings";
public static final String FIRST_USE = "com.whatever.prefs.firstUse";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}
}
dimens.xml:
資源\ XML \的preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Images">
<CheckBoxPreference
android:key="hires"
android:title="Hi-Res Images"
android:summary="Show high quality images. These take longer to load"
android:defaultValue="False" />
</PreferenceCategory>
<PreferenceCategory
android:title="Pie Info">
<CheckBoxPreference
android:key="pie"
android:title="Pie"
android:summary="Like Pie"
android:defaultValue="true" />
<ListPreference
android:dependency="pie"
android:key="pie_type"
android:title="Pie Type"
android:summary="Preferred pie type for eating"
android:dialogTitle="Type of Pie"
android:entries="@array/pie_array"
android:entryValues="@array/pie_array"
android:defaultValue="apple" />
<EditTextPreference
android:key="more_info"
android:title="More Info"
android:summary="More about pies"
android:defaultValue="" />
</PreferenceCategory>
<PreferenceScreen
android:key="second_preferencescreen"
android:title="Second Screen of Settings">
<EditTextPreference
android:key="extraA"
android:title="More Data"
android:summary="Another EditTextPreference"
android:defaultValue="" />
<EditTextPreference
android:key="ExtraB"
android:title="Even More Info"
android:summary="What more can we say"
android:defaultValue="" />
</PreferenceScreen>
</PreferenceScreen>