Android_02_关于SharePreferences的使用
2015-10-15 11:34
701 查看
前言:
我们使用SharePreferences的主要目的是针对一些简单的数据进行存取,其是通过键值对来存取的,
其实质是通过xml文件进行保存的;对于一些简单数据的存取,我们可以用SharePreferences,替代
其他几种复杂的数据存取的方式,比如文件的读写或者数据库的操作;
本示例演示的内容是:通过SharedPreferences来实现记住密码的功能,无需第二次再输入密码;
代码示例如下:
MainActivity:
activity_main.xml:
运行结果如下:
![](https://img-blog.csdn.net/20160625235504449)
总结:
1>关于SharePreferences的写操作:
SharedPreferences sharedPreferences = getSharedPreferences("test",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name",name);
editor.commit();
2>关于SharePreferences的读操作:
SharedPreferences sharedPreferences = getSharedPreferences("test",MODE_PRIVATE);
editTextName.setText(sharedPreferences.getString("name",""));
3>SharePreferences其实质是通过XML文件来进行存取
在本应用的目录下我们可以看到:
![](https://img-blog.csdn.net/20160625235719261)
打开test.xml,可以看到:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="password">123456</string>
<string name="name">will</string>
</map>
源码:
SharePreferences的代码示例源码
我们使用SharePreferences的主要目的是针对一些简单的数据进行存取,其是通过键值对来存取的,
其实质是通过xml文件进行保存的;对于一些简单数据的存取,我们可以用SharePreferences,替代
其他几种复杂的数据存取的方式,比如文件的读写或者数据库的操作;
本示例演示的内容是:通过SharedPreferences来实现记住密码的功能,无需第二次再输入密码;
代码示例如下:
MainActivity:
package com.example.administrator.testsharedpreference; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private EditText editTextName,editTextPassword; private CheckBox checkBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /*** * 获取对应的控件 * **/ editTextName = (EditText) findViewById(R.id.name); editTextPassword = (EditText) findViewById(R.id.password); checkBox = (CheckBox) findViewById(R.id.cb); /*** * 显示用户名和密码 * **/ readCount(); } /*** * 读取用户名和密码 * **/ public void readCount(){ /*** * 通过SharedPreferences读数据分为两步: * 1>取得SharedPreferences对象 * 2>通过SharedPreferences.get...()方法就可以取得相应的数据了; * **/ /*** * test:表示其所存放的文件名为test,其后缀默认是为xml * **/ SharedPreferences sharedPreferences = getSharedPreferences("test",MODE_PRIVATE); /*** * 第一个参数:表示键 * 第二个参数:默认值,表示当通过键没有找到对应的值时,则用这个默认值代替 * **/ editTextName.setText(sharedPreferences.getString("name","")); editTextPassword.setText(sharedPreferences.getString("password","")); } public void logIn(View view){ String name = editTextName.getText().toString(); String password = editTextPassword.getText().toString(); /*** * 登录成功,则弹出Toast,显示登录成功, * 若在此过程中,勾选了记住用户名和密码,则通过SharedPreferences将其保存; * 若登录失败,则弹出Toast,显示登录失败 * ***/ if((name.equals("will"))&&(password.equals("123456"))){ Toast.makeText(this,"log success",Toast.LENGTH_SHORT).show(); /*** * 通过SharedPreference来保存用户名和密码, * 最终其会以test.xml的形式将数据保存下来; * **/ if(checkBox.isChecked()){ /*** * 通过SharedPreferences保存数据分为四步: * 1>取得SharedPreferences对象; * 2>取得SharedPreferences.Editor对象 * 3>通过SharedPreferences.Editor对象的put...()方法进行设置数据; * 4>最后通过SharedPreferences.Editor对象的commit()方法保存数据 * **/ SharedPreferences sharedPreferences = getSharedPreferences("test",MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("name",name); editor.putString("password",password); /*** * 其数据是保存在/data/data/<package_name>/shared_prefs/目录下, * 在本示例中,其保存的文件名为test.xml * **/ editor.commit(); } }else { /** * 显示登录失败 * **/ Toast.makeText(this,"log fail",Toast.LENGTH_SHORT).show(); } } }
activity_main.xml:
<?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:orientation="vertical" tools:context="com.example.administrator.testsharedpreference.MainActivity"> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/name" android:hint="input name" android:layout_gravity="center_horizontal" /> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/password" android:hint="input password" android:layout_gravity="center_horizontal" /> <CheckBox android:layout_width="200dp" android:layout_height="wrap_content" android:text="remember name and password" android:id="@+id/cb" android:layout_gravity="center_horizontal" /> <Button android:layout_width="200dp" android:layout_height="wrap_content" android:text="log in" android:onClick="logIn" android:layout_gravity="center_horizontal" /> </LinearLayout>
运行结果如下:
总结:
1>关于SharePreferences的写操作:
SharedPreferences sharedPreferences = getSharedPreferences("test",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name",name);
editor.commit();
2>关于SharePreferences的读操作:
SharedPreferences sharedPreferences = getSharedPreferences("test",MODE_PRIVATE);
editTextName.setText(sharedPreferences.getString("name",""));
3>SharePreferences其实质是通过XML文件来进行存取
在本应用的目录下我们可以看到:
打开test.xml,可以看到:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="password">123456</string>
<string name="name">will</string>
</map>
源码:
SharePreferences的代码示例源码
相关文章推荐
- android 打包出现错误
- Android RecycleView 使用技巧和心得
- Android Selector 与 Shape 基本用法
- Android中Bitmap和Drawable
- 专题 - Web应用->Web应用中适配不同屏幕
- android获取SDCard中照片并按照时间顺序显示
- Android Bitmap实战技巧
- android:强大的图片下载和缓存库Picasso
- android studio -- 图片应该放在drawable还是mipmap?
- Android -- 使用inBitmap要注意的地方
- 天龙txt阅读器
- Android下的.so库使用
- Android授权登录新浪微博获取用户个人信息
- Android Studio中使用Git进行版本控制
- Android—— LogCat、StrictMode、全局捕获异常
- Android数据库升级
- android 获取图片绝对地址
- Android_02_文件访问权限(待更新)
- MPAndroidChart开源图表《总》之折线图、统计图、扇形图
- Android -- 使用inBitmap要注意的地方