Android登录页面案例(SharedPreferences的使用)
2017-04-10 18:57
295 查看
SharedPreferences是Android平台上一个轻量级的存储器,主要用于存储一些应用程序的配置参数。SharedPreferences中存储的数据是以key/value键值对的形式保存在xml文件中,该文件位于data/data/<packagename>/shared_prefs文件夹中。 注意:SharedPreferences中的value值只能是float、int、long、boolean、String、StringSet类型数据。
接下来我们通过一个案例来介绍它的使用:
1.创建程序
首先创建一个程序,设计用户交互界面,设计如图所示:
该程序对应的布局文件(activity_main.xml和login_top.xml)如下所示:
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:background="#A4D3EE" tools:context="com.example.login123.LoginActivity"> <include layout="@layout/login_top"></include> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/deer" android:id="@+id/imageView" android:layout_alignParentBottom="true" /> </RelativeLayout>
login_top.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="32dp"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/etName" android:drawableLeft="@drawable/icon_user" android:drawablePadding="10dp" android:ems="10" android:hint="请输入账号" android:background="#ffffff"> <requestFocus/> </EditText> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/etPassword" android:layout_below="@id/etName" android:drawableLeft="@drawable/icon_pass" android:drawablePadding="10dp" android:ems="10" android:hint="请输入密码" android:background="#ffffff"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/etPassword"> <CheckBox android:text="记住密码" android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/cbIsRememberPass" android:textSize="20sp" android:layout_weight="1"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#6495ED" android:onClick="login" android:text="登录"/> </LinearLa 4000 yout> </RelativeLayout>
该布局放置了一个相对布局,放置了两个EditText用来输入账号和密码,下方又放了一个线性布局LinearKayout,里面存放了一个ChechBox和一个Button,用来选择是否记住密码和点击登录。
2.编写界面交互代码(LoginActivity)
在LoginActivity中,当用户输入完账号和密码后,选择记住密码,点击“登录”按钮时将信息保存到SharedPreferences中。相应代码如下:
package com.example.login123; 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 cbIsRememberPass; private SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); 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); cbIsRememberPass.setChecked(true); } } public void initViews() { etName = (EditText) findViewById(R.id.etName); etPassword = (EditText) findViewById(R.id.etPassword); cbIsRememberPass = (CheckBox) findViewById(R.id.cbIsRememberPass); } 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 (cbIsRememberPass.isChecked()) { editor.putBoolean("rememberpassword", true); editor.putString("name", name); editor.putString("password", password); } else { editor.clear(); } editor.commit(); Intent intent=new Intent(this,TwoActivity.class); startActivity(intent); finish(); }else { Toast.makeText(this,"账号或密码错误",Toast.LENGTH_LONG); } } }
上面代码中,首先在initView()方法中获取到控件,然后在login方法中实现了单机按钮保存数据。为了增加程序的健壮性,这里加了判断:如果用户名或密码为空或错误是就会弹出一个Toast提示,提示用户名或密码错误。
3.运行程序
运行之后出现下面这个程序,输入账号密码:
点击登录,跳转到欢迎界面(由于欢迎界面代码很简单,所以就不在这里呈现了):
在这个程序中运行之后输入账号密码点击记住密码,登陆之后再退出,下一次打开界面上会记住账号密码,不需要再次输入。若不勾选记住密码就需要再次输入。
打开DDSM窗口,可以在目录下找到保存的文件,打开之后会发现SharedPreferences将用户名和密码以xml的形式保存到了该文件中,同时也可以将该文件导出来。
相关文章推荐
- 使用Android常用控件与布局实现美观的登录页面
- 使用Form验证,未登录时登录页面无法显示图片、css、js文件的解决方法
- 使用busybox的telnetd登录android开发板
- 正则表达式简单使用_登录后跳转至下载页面
- 使用Jquery搭建最佳用户体验的登录页面。(优化点点网登录页面)
- Android使用ViewFlipper做页面切换,与手势滑动切换的使用
- 在jsp页面使用session来验证未经登录的访问 并完成直接跳转
- android开发基础:Intent与Bundle的使用,实现页面跳转及信息传递
- android开发我的新浪微博客户端-登录页面功能篇(4.2)
- android开发我的新浪微博客户端-登录页面UI篇(4.1)
- 使用Jquery搭建最佳用户体验的登录页面 之 记住密码自动登录功能,(含后台代码)
- 登录页面设计案例
- android开发我的新浪微博客户端-登录页面功能篇(4.2)
- android开发我的新浪微博客户端-登录页面UI篇(4.1)
- spring security 2中使用通过自定义过滤器实现多登录页面
- 使用Twitter API来建立登录页面
- android开发我的新浪微博客户端-登录页面功能篇(4.2)
- 使用PHP返回需要登录验证的HTML页面
- 使用C#抓取页面----GET方法,POST方法,抓取登录页面
- 使用Jquery搭建最佳用户体验的登录页面之记住密码自动登录功能(含后台代码)