Android数据存储——登陆案例(记住密码)
来源:互联网 发布:淘宝店招图片怎么做 编辑:程序博客网 时间:2024/06/09 14:57
大部分的登陆界面为了方便客户使用都会有记住密码这个功能。通常都会通过SharedPreferences来实现。SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置。SharedPreferences数据的四种操作模式:
Context.MODE_PRIVATE
Context.MODE_APPEND
Context.MODE_WORLD_READABLE
Context.MODE_WORLD_WRITEABLE
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件.
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取.
MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入
接下来通过一个案例演示如何使用SharedPreference存储数据:
运行效果图:
程序菜单:
一些用到的小图片:
各部分代码:
login_top.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="match_parent" android:background="@drawable/logintop_groungdg" android:padding="@dimen/activity_horizontal_margin"> <EditText android:id="@+id/etName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/icon_user" android:hint="@string/etName" android:drawablePadding="10dp" > <requestFocus/> </EditText> <EditText android:id="@+id/etPassword" android:layout_below="@id/etName" android:inputType="textPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/icon_pass" android:hint="@string/etPassword" android:drawablePadding="10dp" > <requestFocus/> </EditText> <LinearLayout android:layout_below="@id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:text="记住密码" android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/checkBox" android:layout_weight="1" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/btn_select" android:onClick="login" android:text="@string/btnLogin"/> </LinearLayout></RelativeLayout>
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: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" android:background="@drawable/loginbg" tools:context="com.example.administrator.case_login.LoginActivity"> <include layout="@layout/login_top"></include> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/deer" android:id="@+id/imageView" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /></RelativeLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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="com.example.administrator.case_login.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="40sp" android:layout_marginTop="200dp" android:text="welcom you" /></RelativeLayout>
以下三个btn开头xml,是自定义的按钮的样式,可以根据自己喜好设置。
btn_select.xml:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/btn_shape" android:state_pressed="true"></item> <item android:drawable="@drawable/btn_shapeafter" android:state_pressed="true"></item></selector>
btn_shape.xml:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#FF72CAE1"></solid> <corners android:radius="10dp"></corners></shape>
btn_shapeafter.xml:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#87cefa"></solid> <corners android:radius="10dp"></corners></shape>
登陆界面的自定义属性:
loginbg.xml:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"><gradient android:startColor="#FFACDAE5" android:endColor="#FF72CAE1" android:angle="45"/></shape>
logintop_groungdg.xml:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"> <corners android:radius="10dp"></corners> <solid android:color="#55FFFFFF"></solid></shape>
小配置:
strings.xml:
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style></resources>
Class:
LoginActivity:
package com.example.administrator.case_login;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends AppCompatActivity { private EditText etName; private EditText etPassword; private CheckBox checkBox; private SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); intiViews(); sharedPreferences=getSharedPreferences("rememberpassword", Context.MODE_PRIVATE); boolean isRemember= sharedPreferences.getBoolean("rememberpassword",false); if(isRemember){ String name=sharedPreferences.getString("name",""); String password=sharedPreferences.getString("password",""); etName.setText(name); etPassword.setText(password); checkBox.setChecked(true); } } private void intiViews(){ etName=(EditText)findViewById(R.id.etName); etPassword=(EditText)findViewById(R.id.etPassword); checkBox=(CheckBox)findViewById(R.id.checkBox); } public void login(View view){ String name=etName.getText().toString(); String password=etPassword.getText().toString(); if("admin".equals(name)&&"123456".equals(password)){ SharedPreferences.Editor editor=sharedPreferences.edit(); if (checkBox.isChecked()){ editor.putBoolean("rememberpassword",true); editor.putString("name",name); editor.putString("password",password); }else { editor.clear(); } editor.commit(); Intent intent=new Intent(this,MainActivity.class); startActivity(intent); finish(); }else { Toast.makeText(this,"账号密码错误",Toast.LENGTH_LONG).show(); } }}
MainActivity:
import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
只实现了一些简单功能,输入正确的账号密码后选择记住密码,返回主界面后再次进入程序账号密码依然存在,如果没有选择记住密码,再次进入后账号密码就没了。
- Android数据存储——登陆案例(记住密码)
- Android数据存储和访问之SharedPreferences存储方式_用户登录记住密码案例
- android登陆记住密码
- Android:用SharePreferences实现记住密码功能 (登陆案例)
- Android SharedPreferences的轻量级数据存储类的使用 记住密码小案例
- 数据存储--SharedPreferences--仿QQ登陆界面记住密码效果
- Cookie学习总结-登陆案例(记住用户名和密码)
- Android数据存储和访问——用户登录记住密码
- android 登陆时记住密码
- android开发关于sp共享数据存储(记住账号密码储存数据例子)
- android数据存储之sharedPreferences记住密码完成跳转
- Android数据存储和访问-登录记住密码功能
- Android数据存储和访问-登录记住密码功能
- Android数据存储和访问--用户登录记住密码
- 登录案例(记住密码)
- Android—记住密码功能
- 登陆界面,记住密码提交网络数据
- android SharedPreference的简单使用(登陆界面记住密码)
- Linux下结构体task_struct
- HTTP文件上传实例
- python的奇怪小地方 in numpy
- HDU5423 Rikka with Tree(树,深搜DFS)
- java 运行 linux shell 命令
- Android数据存储——登陆案例(记住密码)
- II2S通信学习(待完成)
- ACM A+B Problem
- java可视化编程-eclipse安装windowbuilder插件
- swust.oj.1076
- 并查集_hdu1213
- React-Native 与原生的3种交互通信(Android)
- div与span及 block-line块元素与in-line内联元素的区别(H5)
- 动态规划0—1背包问题