Android-数据存储SharedPreferences的使用
来源:互联网 发布:网络属于强电还是弱电 编辑:程序博客网 时间:2024/05/16 16:20
SharedPreferences的使用步骤:
1、得到SharedPreferences对象。
2、调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象。
3、向SharedPreferences.Editor对象中添加数据。
4、调用commit方法将添加的数据提交。
案例:用户登录记住密码
1、login_activity.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/drawable_rounder" android:padding="@dimen/activity_horizontal_margin"> <EditText android:id="@+id/EtName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/etname" android:ems="10" android:drawablePadding="10dp" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/icon_user" > <requestFocus/> </EditText> <EditText android:id="@+id/EtPass" android:layout_below="@id/EtName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/etpass" android:drawablePadding="10dp" android:inputType="textPassword" android:ems="10" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/icon_pass"> <requestFocus/> </EditText> <CheckBox android:id="@+id/cbpass" android:layout_below="@id/EtPass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" android:textSize="20dp" android:layout_margin="5dp" android:checked="false"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/cbpass"> <Button android:id="@+id/btn" android:onClick="login" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/de" android:layout_marginRight="10dp" android:background="@drawable/bt_press" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/zhuce" android:background="@drawable/bt_press" /> </LinearLayout></RelativeLayout>
2、activity_login.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:background="@drawable/loginbg" android:id="@+id/activity_login" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="bzu.edu.cn.case_login.LoginActivity"><include layout="@layout/loginlayout"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/deer" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginRight="23dp" android:layout_marginEnd="23dp" android:id="@+id/imageView" /></RelativeLayout>
package bzu.edu.cn.case_login;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends AppCompatActivity { private EditText Ename,Epass; private CheckBox cb; private Button btn; SharedPreferences sharedPreferences; SharedPreferences.Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取控件 Ename=(EditText)findViewById(R.id.EtName); Epass=(EditText)findViewById(R.id.EtPass); cb=(CheckBox)findViewById(R.id.cbpass); //通过getSharedPreferences()方法获取SharePreferences的实例对象 sharedPreferences=getSharedPreferences("Login",MODE_PRIVATE); //获取Editor实例对象,存储数据 editor=sharedPreferences.edit(); //获取数据 String name=sharedPreferences.getString("name",""); String pass=sharedPreferences.getString("pass",""); //判断文本框是否为空 if(name==null&&pass==null){ cb.setChecked(false); } else{ cb.setChecked(true); Ename.setText(name); Epass.setText(pass); } } public void login(View view){ //获取文本框的内容 String name = Ename.getText().toString().trim(); String pass=Epass.getText().toString().trim(); //检验用户名和密码是否正确 if("admin".equals(name)&&"123456".equals(pass)){ if(cb.isChecked()){ //保存数据 editor.putString("name",name); editor.putString("pass",pass); editor.commit(); //提交数据 }else{ editor.clear(); //删除所有数据 editor.commit(); //提交修改 } Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show(); }else{ Toast.makeText(this,"用户名或密码错误",Toast.LENGTH_LONG).show(); } }}
0 0
- Android-数据存储SharedPreferences的使用
- Android的数据存储方式-------使用SharedPreferences存储数据
- Android 使用SharedPreferences数据存储
- android的数据存储 SharedPreferences
- Android的数据存储--SharedPreferences .
- Android的数据存储--SharedPreferences
- Android的数据存储--SharedPreferences
- Android的数据存储--SharedPreferences
- Android SharedPreferences的数据存储
- Android存储数据的方法----SharedPreferences的使用
- android中,使用SharedPreferences数据存储后文件的位置
- 【Android SharedPreference】 使用SharedPreferences存储复杂类型的数据
- Android 数据存储与IO之SharedPreferences的使用
- Android开发中SharedPreferences存储数据的使用
- Android数据存储SharedPreferences的使用场景和注意事项
- android中使用SharedPreferences进行数据存储的操作方法
- Android数据存储之SharedPreferences的封装使用
- Android数据存储——SharedPreferences的使用
- c#134课的主要内容
- 小白:从0开始捣鼓Ubuntu(修改下载源和一些自己的设置)
- jQuery-常用工具函数
- 解决excel中的讨厌的科学计数法
- MySql5.7 全文索引(针对中文搜索)
- Android-数据存储SharedPreferences的使用
- 安装gsl 科学计算库 亲测可用
- 图书购买系统之本地添加购物车(完善版)--实现生成订单和子订单
- Ubuntu新手-Ubuntu 各个目录的含义
- CAFFE源码学习笔记之池化层pooling_layer
- centos配置nat桥接并配置静态ip
- windows使用的各种字符串
- JavaScript reduce用法
- 克隆二叉树