大部分的登陆界面为了方便客户使用都会有记住密码这个功能。通常都会通过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);
}
}
只实现了一些简单功能,输入正确的账号密码后选择记住密码,返回主界面后再次进入程序账号密码依然存在,如果没有选择记住密码,再次进入后账号密码就没了。