本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/
下面展示如何使用SharedPreferences对象去保存应用的数据。你也将会看见通过特殊的Activity去修改已经被保存的应用数据。
1. 新建一个工程,UsingPreferences。
2. 在res文件夹下面新建一个文件夹,xml。在新建的文件夹中新建一个文件,myappreferences.xml。
3. 在myapppreferences.xml文件中编写代码。
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Category 1">
<CheckBoxPreference
android:title="Checkbox"
android:defaultValue="false"
android:summary="True or False"
android:key="checkboxPref" />
</PreferenceCategory>
<PreferenceCategory android:title="Category 2">
<EditTextPreference
android:summary="Enter a string"
android:defaultValue="[Enter a string here]"
android:title="Edit Text"
android:key="editTextPref"
/>
<RingtonePreference
android:summary="Select a ringtone"
android:title="Ringtones"
android:key="ringtonePref"
/>
<PreferenceScreen
android:title="Second Preference Screen"
android:summary=
"Click here to go to the second Preference Screen"
android:key="secondPrefScreenPref" >
<EditTextPreference
android:summary="Enter a string"
android:title="Edit Text (second Screen)"
android:key="secondEditTextPref"
/>
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
4. 在包路径下面新建一个类,AppPreferenceActivity。
5. AppPreferenceActivity中的代码。
public class AppPreferenceActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager prefMgr = getPreferenceManager();
prefMgr.setSharedPreferencesName("appPreferences");
//---load the preferences from an XML file---
addPreferencesFromResource(R.xml.myapppreferences);
}
}
6. 在AndroidManifest.xml中添加AppPreferenceActivity类的入口。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.manoel.UsingPreferences"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".UsingPreferencesActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AppPreferenceActivity"
android:label="@string/app_name">
<intent-filter>
<action
android:name="net.manoel.AppPreferenceActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
7. main.xml中的代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnPreferences"
android:text="Load Preferences Screen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickLoad"/>
<Button
android:id="@+id/btnDisplayValues"
android:text="Display Preferences Values"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickDisplay"/>
<EditText
android:id="@+id/txtString"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnModifyValues"
android:text="Modify Preferences Values"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickModify"/>
</LinearLayout>
8. 在UsingPreferencesActivity.java中添加一些代码。
public class UsingPreferencesActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClickLoad(View view) {
Intent i = new Intent("net.manoel.AppPreferenceActivity");
startActivity(i);
}
}
9. 按F11在模拟器上面测试。点击最上面的按钮,跳转到UsingPreferences这个界面。
10. 点击CheckBox,让它的状态在checked和unchecked之间进行切换。观察CATEGORY 1和CATEGORY 2。点击Edit Text,输入一些字符,然后按OK把对话框消失掉。
11. 点击Ringtones,选择默认的铃声或静音。如果在真机上面测试的话,可能会有更多的选项。
12. 点击Second Preference Screen,就会跳转到下一屏。
13. 如果想要返回上一屏,可以按Back键。
14. 一旦你修改了选项中的任何一项,在/data/data/net.manoel.UsingPreferences/shared_prefs文件夹下面就会新建一个文件。可以使用DDMS进行查看。
15. 如果你把这个文件打开的话,会看见类似的内容。
分享到:
相关推荐
Android实现 点击EditText编辑框清除hint预设字 的功能实现Demo。
来自滨田英明(Hideaki Hamada)灵感的日系电影情绪心情LR预设+Capture One预设 这款柔和而简约的预设背包灵感来自日本的主要摄影师滨田秀明(Hideaki Hamada)。 这些预设将帮助您添加干净的色调,日式摄影风格的情绪...
vlc-android原始代码-预设了只剩下Android部分 ubuntu 15.04编译Android-VLC 查看本文之前,请先看VLC官网: wiki.videolan.org/AndroidCompile 编译VLC在网络上已经有很多人在说了,我是站在前人的基础上编译的,...
《LR预设文件转换》用于将LR预设文件(扩展名为 lrtemplate的文件)转换为ACR的预设文件(扩展名为xmp的文 件)。 二、使用条件 1.电脑中装有Photoshop软件,并有ACR插件。 2.有LR预设文件(扩展名为lrtemplate的...
D610机内预设.
这些预设适用于移动 lightroom 应用程序(iOS 和 Android),也适用于桌面版 lightroom。 Lightroom 的 5 个移动预设(.DNG 文件)-5x 桌面预设(.xmp 文件)- 5x LUT 用于视频编辑(.CUBE 文件)
各种PS预设和LR预设,里面有我自己制作的各种预设
此外,您可以混合和匹配设置来开发自己的彩色调色板!【1】夜间摄影 预设:许多即时的WOW效果从未见过的色彩解决方案。【2】神奇的柔软度预设:以更柔和,更具侵略性的色彩处理方式。【3】日落和日出 预设:具有新的...
Camera Raw 预设 #1 “VSCO” VSCO 预设现在非常受欢迎,给照片带来了一些怀旧感和完美的老式修饰。借助多功能调色板,您将获得柔和的肤色、柔和的色彩和自然的肤色。 推荐照片:婚礼、时尚、肖像、旅行、儿童、室内...
将您的预设文件夹复制到开发预设文件夹中 重新启动 Lightroom 如何为 Mac 安装 Lightroom 4、5、6 和 CC 预设: 打开 Lightroom 转到:编辑•首选项•预设 点击标题为:显示 Lightroom 的预设文件夹 双击 Lightroom...
EDIUS调色预设
Preset Premium v2.3.0 for Android 为 Lightroom 提供高质量预设滤镜效果
VTemplate泛型集合字典根据预设键获取值
ps简单动作预设PSD
MBLooks预置安装方法: 直接复制【MBLooks 新增预置】文件夹下的所有 文件夹 粘贴到 ...Magic Bullet Looks 1.4自带了11组预设,现在又新增了6组预设上100种效果。选择其中一种预设,双击它就可以对原素材调色。
PS中CAMERA的ACR预设,看看不错的选择,懒人预设,看看不错的选择,懒人预设,看看不错的选择,懒人预设
滤镜君「Koloro Premium」v4.9.5 for Android 一个 Lr mobile 预设照片编辑器的理想工具
looks 预设.rarlooks 预设.rar
pr无缝转场预设(含教程).rar
这是capture one 的预设,这个预设在网上不多见,所以大家可以作为借鉴去调色