网站开发外包计入什么科目制作网页是什么专业
news/
2025/9/27 9:39:36/
文章来源:
网站开发外包计入什么科目,制作网页是什么专业,南京百度关键字优化价格,做头像的网站空白前言
Android的数据存储机制中还提供了SharedPreferences#xff0c;SharedPreferences是这其中最容易理解的数据存储技术#xff0c;采用键值对的方式进行存储#xff0c;而且支持存储多中数据类型。 获取SharedPreferences对象
SharedPreferences文件存放在/data/data/SharedPreferences是这其中最容易理解的数据存储技术采用键值对的方式进行存储而且支持存储多中数据类型。 获取SharedPreferences对象
SharedPreferences文件存放在/data/data/package name/shared_prefs/中在Android的中主要提供了三种方式进行SharedPreferences对象的获取。
1.Context中的getSharedPreferences()方法
Override
public SharedPreferences getSharedPreferences(String name, int mode) {return mBase.getSharedPreferences(name, mode);
}
2.Activity中的getPreferences()
/*** Retrieve a {link SharedPreferences} object for accessing preferences* that are private to this activity. This simply calls the underlying* {link #getSharedPreferences(String, int)} method by passing in this activitys* class name as the preferences name.* * param mode Operating mode. Use {link #MODE_PRIVATE} for the default * operation, {link #MODE_WORLD_READABLE} and * {link #MODE_WORLD_WRITEABLE} to control permissions.** return Returns the single SharedPreferences instance that can be used* to retrieve and modify the preference values.*/
public SharedPreferences getPreferences(int mode) {return getSharedPreferences(getLocalClassName(), mode);
}
3.PreferenceManager中的getDefaultSharedPreferences()
/*** Gets a SharedPreferences instance that points to the default file that is* used by the preference framework in the given context.* * param context The context of the preferences whose values are wanted.* return A SharedPreferences instance that can be used to retrieve and* listen to values of the preferences.*/
public static SharedPreferences getDefaultSharedPreferences(Context context) {return context.getSharedPreferences(getDefaultSharedPreferencesName(context),getDefaultSharedPreferencesMode());
} SharedPreferences进行存储
获取了SharedPreferences对象进行存储操作就简单了实例如下
public void onClick(View v) {SharedPreferences.Editor editor getSharedPreferences(data, MODE_PRIVATE).edit();editor.putString(name, Xiaoming);editor.putInt(age, 18);editor.commit();Toast.makeText(getApplicationContext(), SP写入成功, Toast.LENGTH_SHORT).show();
}
这是存储后生成的文件
?xml version1.0 encodingutf-8 standaloneyes ?
mapboolean namemarried valuefalse /string namenameXiaoming/stringint nameage value18 /
/mapSharedPreferences进行读取
读取同样简单代码如下
public void onClick(View v) {SharedPreferences pref getSharedPreferences(data, MODE_PRIVATE);String str pref.getString(name, ) \n pref.getInt(age, 0);Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
} 总结
通过上面的代码可以很清楚的了解SharedPreferences的使用方法也可以看到SharedPreferences比文件存储要方便很多。 博客名称王乐平博客 博客地址http://blog.lepingde.com CSDN博客地址http://blog.csdn.net/lecepin
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/916720.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!