Android_02_关于SharePreferences的使用
来源:互联网 发布:扫描物品价值软件 编辑:程序博客网 时间:2024/05/29 10:48
前言:
我们使用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_02_关于SharePreferences的使用
- 关于sharePreferences的用法。。。。
- Android SharePreferences 的使用读写
- (Android review)SharePreferences的使用
- Android之SharePreferences的使用
- 使用SharePreferences
- 使用SharePreferences进行数据的存储
- Android_02_获取SD卡的可用容量
- cocos2d-android_02_熟悉基本的类
- 轻量级数据库使用Sharepreferences
- Android数据的四种存储方式 SharePreferences使用
- Android数据存储之SharePreferences的使用总结
- SharePreferences的用法
- SharePreferences类的运用
- sharePreferences的简单封装
- SharePreferences
- SharePreferences
- SharePreferences
- setContentView(R.layout.activity_main)无法正常引用
- MySQL索引原理与慢查询优化
- ios 设置声音和震动,单独控制
- CentOS6.5连接Wifi详细步骤及常见问题解答
- android 打包出现错误
- Android_02_关于SharePreferences的使用
- show 2秒第二种实现
- 机器学习(八)前馈神经网络
- 手游页游和端游的服务端的架构与区别
- Linux内核Code Review 001
- python 装饰器
- 百练 2755 神奇的口袋 dp背包
- Ralink RT5350:添加自定义GPIO应用程序
- 【Unity】获取Xml中Attributes的描述文字