Android 使用SharedPreferences对象保存账号密码
2017-12-13 10:51
429 查看
在项目中,常常需要保存用户的账号密码,方便用户一键登录。
我使用的是SharedPreferences进行保存。
SharedPreferences是一种轻量级的数据存储方式,本质是基于XML文件存储key-value键值对数据。我们可以通过edit()方法修改内容,通过Commit()方法来提交修改后的内容。
SharedPreferences的存储步骤一般为四步:
根据Context获取SharedPreferences对象
利用edit()方法获取Editor对象
通过Editor对象存储key-value键值对数据
通过commit()方法提交数据
SharedPreferences对象本身只能够获取数据,而不支持存储和修改,存储修改是由Editor对象实现的。SharedPreferences只能存储boolean、int、float、long、String五种简单的数据类型,一般会将复杂类型的数据转换成Base64编码,然后将转换后的数据以字符串的形式保存在 XML文件中,再用SharedPreferences保存,无法进行条件查询。
SharedPreferences是一个接口,无法创建实例,我们通过Context.getSharedPreferences(String name,int mode)来得到一个SharedPreferences实例。name是文件名字,mode是读写方式。
mode有三种:
Context.MODE_PRIVAT(指定该SharedPreferences的数据只能被本应用程序读写)
Context.MODE_WORLD_READABLE(指定该SharedPreferences的数据能被其他应用程序读,但不能写)
Context.MODE_WORLD_WROTEABLE(指定该SharedPreferences的数据能被其他应用程序读写)
下面是使用SharedPreferences保存账号密码的方法:
首先先写个简单的登录界面:
activity_main.xml
MainActivity.class
最后效果如图:
我使用的是SharedPreferences进行保存。
SharedPreferences是一种轻量级的数据存储方式,本质是基于XML文件存储key-value键值对数据。我们可以通过edit()方法修改内容,通过Commit()方法来提交修改后的内容。
SharedPreferences的存储步骤一般为四步:
根据Context获取SharedPreferences对象
利用edit()方法获取Editor对象
通过Editor对象存储key-value键值对数据
通过commit()方法提交数据
SharedPreferences对象本身只能够获取数据,而不支持存储和修改,存储修改是由Editor对象实现的。SharedPreferences只能存储boolean、int、float、long、String五种简单的数据类型,一般会将复杂类型的数据转换成Base64编码,然后将转换后的数据以字符串的形式保存在 XML文件中,再用SharedPreferences保存,无法进行条件查询。
SharedPreferences是一个接口,无法创建实例,我们通过Context.getSharedPreferences(String name,int mode)来得到一个SharedPreferences实例。name是文件名字,mode是读写方式。
mode有三种:
Context.MODE_PRIVAT(指定该SharedPreferences的数据只能被本应用程序读写)
Context.MODE_WORLD_READABLE(指定该SharedPreferences的数据能被其他应用程序读,但不能写)
Context.MODE_WORLD_WROTEABLE(指定该SharedPreferences的数据能被其他应用程序读写)
下面是使用SharedPreferences保存账号密码的方法:
首先先写个简单的登录界面:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name:"/> <EditText android:layout_marginLeft="10dp" android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入账号"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:"/> <EditText android:layout_marginLeft="10dp" android:id="@+id/password" android:inputType="textPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="保存"/> <Button android:layout_weight="1" android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提取"/> </LinearLayout> <TextView android:id="@+id/get_name" android:layout_width="match_parent" android:layout_height="50dp" /> <TextView android:id="@+id/get_password" android:layout_width="match_parent" android:layout_height="50dp" /> </LinearLayout>
MainActivity.class
public class MainActivity extends AppCompatActivity { private Button mSave; private Button mGet; private EditText mName; private EditText mPassword; private SharedPreferences sharedPreferences = null; private TextView getName; private TextView getPassword; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mName = (EditText) findViewById(R.id.name); mPassword = (EditText) findViewById(R.id.password); getName = (TextView) findViewById(R.id.get_name); getPassword = (TextView) findViewById(R.id.get_password); mSave = (Button) findViewById(R.id.btn); mGet = (Button) findViewById(R.id.btn2); mSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { save(); } }); mGet.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { get(); } }); } /** * 保存账号密码 */ private void save(){ sharedPreferences = getSharedPreferences("name",MODE_PRIVATE); //得到Editor对象 SharedPreferences.Editor edit = sharedPreferences.edit(); //记录账号 edit.putString("name",mName.getText().toString()); //记录密码 edit.putString("password",mPassword.getText().toString()); //提交 edit.commit(); } /** * 提取账号密码 */ private void get(){ sharedPreferences = getSharedPreferences("name",MODE_PRIVATE); getName.setText("账号: " + sharedPreferences.getString("name","")); getPassword.setText("密码: " + sharedPreferences.getString("password","")); } }
最后效果如图:
相关文章推荐
- Android使用SharedPreferences保存账号密码
- Android中使用sharedPreferences对登陆页面中的账号密码进行保存
- Android 使用SharedPreferences 进行保存账号or密码or其它数据
- 在ios开发中,使用钥匙来保存用户密码和账号
- android登陆界面保存账号密码附带源码
- android--内部存储写文件和使用API获取内部存储路径,sharedperferance获取原有的账号密码--简单的登陆(记录原有的账号和密码)--I/0流介绍
- Mac电脑使用:删除保存在Mac电脑自带的Safari浏览器、谷歌浏览器、火狐浏览器中的账号和密码的方法
- android SharedPreferences实现用户的注册和保存账号密码
- Android 使用SharedPreferences保存各种数据(List,Map,对象等)+ SpUtils工具
- eclipse 中main()函数中的String[] args如何使用?通过String[] args验证账号密码的登录类?静态的主方法怎样才能调用非static的方法——通过生成对象?在类中制作一个方法——能够修改对象的属性值?
- android 关于使用SharedPreferences来保存对象里既有属性又有集合的问题
- SSH项目中在action中使用cookie保存账号和密码
- Android:使用密码技术安全地保存凭证
- Android 使用LitePal将list(String,对象,集合)数据保存到本地数据库
- android 使用sharedpreferences 保存对象
- android开发学习之 简单的demo SharedPreferences 的登陆账号密码保存和按键退出提醒
- android登录之后保存账号密码
- Git 使用技巧 - 自动保存自己的账号密码
- 使用文件保存数据以及在此基础上实现判断登陆账号密码匹配问题的实现
- Android使用SharedPreferences保存对象