SharedPreferences类的使用
SharedPreferences,用xml文件保存用户的偏好设置,是一个轻量级的存储类。
效果图:
代码:
activity_main
<?xml version="1.0" encoding="utf-8"?><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:gravity="center" tools:context=".MainActivity">
<TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Hello_World" /> </LinearLayout>
activity_settings
<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.example.admin.sharedpreferences.SettingsActivity$preferenceFragment" android:id="@+id/preference"/>
menu.menu
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/menu_settings" android:title="@string/menu_title" app:showAsAction="never"/></menu>
arrays
<?xml version="1.0" encoding="utf-8"?><resources> <string-array name="color_show"> <item>@string/black</item> <item>@string/red</item> <item>@string/blue</item> <item>@string/green</item> </string-array></resources>
preferences_settings
<?xml version="1.0" encoding="utf-8"?><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <CheckBoxPreference android:title="@string/CheckBoxPreference_title" android:defaultValue="false" android:summaryOn="@string/CheckBoxPreference_summaryOn" android:summaryOff="@string/CheckBceoxPreferen_summaryOff" android:key="@string/CheckBceoxPreferen_key"/> <EditTextPreference android:title="@string/EditTextPreference_title" android:defaultValue="@string/EditTextPreference_defaultValue" android:inputType="numberDecimal" android:selectAllOnFocus="true" android:key="@string/EditTextPreference_key"/> <ListPreference android:title="@string/ListPreference_title" android:defaultValue="@string/ListPreference_defaultValue" android:entries="@array/color_show" android:entryValues="@array/color_show" android:key="@string/ListPreference_key"/></PreferenceScreen>
AndroidMainFest
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.admin.sharedpreferences"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SettingsActivity" android:parentActivityName=".MainActivity"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity"/> </activity> </application> </manifest>
MainActivity.java
package com.example.admin.sharedpreferences; import android.content.Intent;import android.content.SharedPreferences;import android.graphics.Color;import android.preference.PreferenceManager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textview); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case R.id.menu_settings: Intent intent = new Intent(this, SettingsActivity.class); startActivity(intent); break; } return true; } @Override protected void onStart() { super.onStart(); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); if (sharedPreferences.getBoolean(getString(R.string.CheckBceoxPreferen_key), false)) { textView.setText(R.string.Bye); } String colorText = sharedPreferences.getString(getString(R.string.ListPreference_key), getString(R.string.ListPreference_defaultValue)); String[] color_array = getResources().getStringArray(R.array.color_show); for (int i = 0; i < color_array.length; i++) { if (color_array[i] .equals(colorText)){ setTextColor(i); break; } } String sizeText = sharedPreferences.getString(getString(R.string.EditTextPreference_key), getString(R.string.EditTextPreference_defaultValue)); int size = Integer.parseInt(sizeText); textView.setTextSize(size); } public void setTextColor(int number) { switch (number) { default: textView.setTextColor(Color.BLACK); case 0: textView.setTextColor(Color.BLACK); break; case 1: textView.setTextColor(Color.RED); break; case 2: textView.setTextColor(Color.BLUE); break; case 3: textView.setTextColor(Color.GREEN); break; } }}
SettingsActivity.java
package com.example.admin.sharedpreferences; import android.content.SharedPreferences;import android.os.Bundle;import android.preference.ListPreference;import android.preference.Preference;import android.preference.PreferenceFragment;import android.preference.PreferenceManager;import android.support.v4.app.NavUtils;import android.support.v7.app.AppCompatActivity; public class SettingsActivity extends AppCompatActivity { @Override public void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_settings); } @Override public void onBackPressed() { super.onBackPressed(); NavUtils.navigateUpFromSameTask(this); } public static class preferenceFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener { @Override public void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences_settings); Preference preference_size = findPreference(getString(R.string.EditTextPreference_key)); preferenceSummary(preference_size); Preference preference_color = findPreference(getString(R.string.ListPreference_key)); preferenceSummary(preference_color); } @Override public boolean onPreferenceChange(Preference preference, Object newValue) { String value=String.valueOf(newValue); if (preference instanceof ListPreference){ ListPreference listPreference=(ListPreference) preference; int index=listPreference.findIndexOfValue(value); if (index >=0){ CharSequence[] text=listPreference.getEntries(); preference.setSummary(text[index]); } }else { preference.setSummary(value); } return true; } public void preferenceSummary(Preference preference) { preference.setOnPreferenceChangeListener(this ); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(preference.getContext()); String text = sharedPreferences.getString(preference.getKey(), ""); onPreferenceChange(preference,text); } } }
github项目源码: https://github.com/NeoWu55/Android-Sharedpreferences
SharedPreferences类的使用的更多相关文章
- Android学习之SharedPreferences类
SharedPreferences类 android.content.SharedPreferences 类概括: 访问和修改由函数getSharedPreferences(String,int)返回 ...
- 【Android】19.1 SharedPreferences类
分类:C#.Android.VS2015: 创建日期:2016-03-05 一.简介 SharedPreferences:简单共享存储首选项.实际上就是用加密的内部文件保存所有页面都能访问的一系列“n ...
- 安卓SharedPreferences类的使用
package com.lidaochen.phonecall; import android.content.Intent; import android.content.SharedPrefere ...
- Android数据存储方式--SharedPreferences
Android数据存储方式有如下四种:SharedPreferences.存储到文件.SQLite数据库.内容提供者(Content provider).存储到网络服务器. 本文主要介绍一下Share ...
- Android——配置文件的保存SharedPreferences进行数据存储
很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为好友.对于软件配置参数的保存,如果是window软件通常我们会采用ini文件进行保存,如果 ...
- SharedPreferences详解
我们在开发软件的时候,常需要向用户提供软件参数设置功能,例如我们常用的微信,用户可以设置是否允许陌生人添加自己为好友. 对于软件配置参数的保存, 如果是在window下通常我们会采用ini文件进行保存 ...
- 使用SharedPreferences进行数据存储
使用SharedPreferences进行数据存储 很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为好友.对于软件配置参数的保存,如果是w ...
- android开发之路11(用SharedPreferences存储数据)
Android平台给我们提供了一个SharedPreferences类,实际上SharedPreferences处理的就是一个key-value(键值对),它是 一个轻量级的存储类,特别适合用于保存软 ...
- Android数据存储方式之SharedPreferences
Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数.使用SharedPreferences保存数据,其背后是用xml文件存放数 ...
随机推荐
- php ip2long负数的问题
大家可能都知道php提供了ip2long与long2ip方法对ip地址处理.抛砖引玉,说点概念性滴: 1.ip2long — 将一个IPV4的字符串互联网协议转换成数字格式 int ip2long ( ...
- Java开发笔记(十)一元运算符的技巧
前面讲到赋值运算符的时候,提到“x = x+7”可以被“x += 7”所取代,当然Java编程中给某个变量自加7并不常见,常见的是给某变量自加1,就像走台阶,一般都是一级一级台阶地走,犯不着一下子跳上 ...
- 解决Azure 消息队列ServiceBus提示证书不信任无权限的问题
笔者在C# 消息队列-Microsoft Azure service bus 服务总线中使用Azure消息队列,最近偶尔会遇到请求数据不入队列的问题,查找日志,问题如下: 异常:X. certific ...
- 聊聊 API Gateway 和 Netflix Zuul
最近参与了公司 API Gateway 的搭建工作,技术选型是 Netflix Zuul,主要聊一聊其中的一些心得和体会. 本文主要是介绍使用 Zuul 且在不强制使用其他 Neflix OSS 组件 ...
- Android细笔记--DataStorage
Shared Preferences 即使应用被杀了,shared preference也还是存在的 Internal Storage 创建于internal的文件只对本应用开放权限,即使手机用户本身 ...
- 微信小程序(四) 模板的使用
模板的使用:单独建立一个页面,在另一个页面通过name属性名调用使用(注意要导入模板路径) template.wxml页面
- 第三篇 Html-label标签
label标签 用户获取文字,使得关联的标签获取光标 <!DOCTYPE html> <html lang="en"> <head> <m ...
- JIRA笔记(一):安装部署JIRA
(一) 说明 说明JIRA的安装及破解. 操作系统:WIN 10 数据库:Oracle 12C R2(这个版本的jira,atlassian建议的是 12C R1,不过R2也能用,其他版本不清 ...
- restful api与传统api的区别(方式及语法)
示例:一个状态数据操作接口 传统模式: api/getstate.aspx- 获取状态信息api/updatestate.aspx - 更新状态信息api/deletestate.aspx - 删除该 ...
- java使用synchronized与Semaphore解决生产者消费者问题对比
一.synchronized与信号量Semaphore简介 1.synchronized是java中的关键字,是用来控制线程同步的问题最常用的方法. 2.Semaphore是属于java的一个类,同样 ...