SharedPreference 是Android 平台上一个轻量级的存储类,主要用于存储一些应用程序的配置参数,例如用户名,密码,自定义参数的设置等,这次就来解析是如何实现用户账号,密码的自定义操作。
运行效果图:
界面设计,代码如下:
<?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/logintop_roundbg"
android:padding="@dimen/activity_horizontal_margin">
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/edit_text"
android:drawableLeft="@drawable/icon_user"
android:drawablePadding="10dp"
android:ems="10"
android:hint="@string/etName">
<requestFocus />
</EditText>
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/etName"
android:background="@android:drawable/edit_text"
android:drawableLeft="@drawable/icon_pass"
android:drawablePadding="10dp"
android:ems="10"
android:hint="@string/etPass"
android:inputType="textPassword">
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/etPassword">
<CheckBox
android:id="@+id/IsRemember"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="记住密码"
android:textSize="20sp" />
<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>
加入图片的代码:
<?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:background="@drawable/loginbg"
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="cn.edn.bzu.case_login.LoginActivity">
<include layout="@layout/login_top"></include>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="13dp"
android:layout_marginRight="13dp"
app:srcCompat="@drawable/deer" />
</RelativeLayout>
添加交换页面:
<?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="cn.edn.bzu.case_login.MainActivity">
<TextView
android:text="Welcome You Lerarn Android"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="201dp"
android:id="@+id/textView" />
</RelativeLayout>
界面设计完成之后就是编写界面交互代码,在MainActivity中,实现当用户输入完账号和密码后,选择记住密码,单击登陆按钮时调用保存方法保存账号密码,代码如下:
<?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/logintop_roundbg"
android:padding="@dimen/activity_horizontal_margin">
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/edit_text"
android:drawableLeft="@drawable/icon_user"
android:drawablePadding="10dp"
android:ems="10"
android:hint="@string/etName">
<requestFocus />
</EditText>
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/etName"
android:background="@android:drawable/edit_text"
android:drawableLeft="@drawable/icon_pass"
android:drawablePadding="10dp"
android:ems="10"
android:hint="@string/etPass"
android:inputType="textPassword">
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/etPassword">
<CheckBox
android:id="@+id/IsRemember"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="记住密码"
android:textSize="20sp" />
<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>
当程序完成后可以查看自己保存的文件:通过rememberpassword查看的文件
当用户选择保存密码以后,关闭程序后再打开就不用再次输入账号和密码,就如同我们常用的QQ保存账号密码一样.
附带截图: