用户登录记住密码
来源:互联网 发布:数据统计与分析专业 编辑:程序博客网 时间:2024/05/01 18:28
我下面介绍的是记住密码的用户登录,以下就是此实验:
一、实验原理
实现用户名和密码的功能是通过SharedPreferences存储实现的。当用户选中了记住密码复选框,并成功登录一次后,这个时候如果再重新启动登录界面。之前输入的用户名密码就会显示在文本框中,就会被保存,下次登录就不用再用密码
二、训练项目
掌握SharedPreferences的使用。
三、案例实现
登录对应的布局文件Activity_login.xml
[html] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 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="#ADD8E6"
- android:orientation="vertical"
- tools:context="com.example.bz0209.login.LoginActivity">
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="200dp"
- android:background="@drawable/toproundbg"
- android:padding="@dimen/activity_horizontal_margin"
- android:id="@+id/shitu">
- <EditText
- android:id="@+id/etName"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:drawableLeft="@drawable/icon_user"
- android:ems="10"
- android:hint="输入账号:"
- android:drawablePadding="10dp"/>
- <requestFocus/>
- <EditText
- android:id="@+id/etPass"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/etName"
- android:drawableLeft="@drawable/icon_pass"
- android:drawablePadding="10dp"
- android:ems="10"
- android:hint="输入密码:"/>
- <requestFocus/>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/etPass"
- android:layout_marginTop="10dp"
- >
- <CheckBox
- android:layout_marginTop="10dp"
- android:id="@+id/etCheckBox"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="记住密码"
- />
- <Button
- android:id="@+id/etLogin"
- android:layout_marginLeft="90dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/btn_shape"
- android:onClick="btnClick"
- android:text="登录"/>
- </LinearLayout>
- </RelativeLayout>
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="100dp"
- android:layout_marginLeft="100dp"
- android:background="@drawable/deer"
- />
- </LinearLayout>
[html] view plain copy
- <?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_content"
- 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.bz0209.login.ContentActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="内容"
- android:textSize="30sp"
- android:textColor="@color/colorAccent"/>
- </RelativeLayout>
[html] view plain copy
- <span style="font-size:18px;">界面交互界面LoginActivity</span>
[html] view plain copy
- package com.example.bz0209.login;
- import android.content.DialogInterface;
- 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.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.Toast;
- public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
- private EditText etName=null;
- private EditText etPass=null;
- private Button etLogin=null;
- private CheckBox etCheckBox=null;
- SharedPreferences sharedPreferences=null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- sharedPreferences=getSharedPreferences("userInfo",MODE_PRIVATE);
- initView();
- }
- private void initView() {
- etName=(EditText) findViewById(R.id.etName);
- etPass=(EditText)findViewById(R.id.etPass);
- etLogin=(Button)findViewById(R.id.etLogin);
- etCheckBox=(CheckBox)findViewById(R.id.etCheckBox);
- if(sharedPreferences.getBoolean("checkboxBoolean",false)){
- etName.setText(sharedPreferences.getString("etName",""));
- etPass.setText(sharedPreferences.getString("etPass",""));
- etCheckBox.setChecked(true);
- }
- findViewById(R.id.etLogin).setOnClickListener(this);
- }
- public void onClick(View view){
- String Name=etName.getText().toString();
- String Pass=etPass.getText().toString();
- if(Name.isEmpty()){
- Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT);
- return;
- }
- if(Pass.isEmpty()){
- Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT);
- return;
- }
- if("gls".equals(Name)&&"123".equals(Pass){
- boolean CheckLogin=etCheckBox.isChecked();
- if(CheckLogin){
- SharedPreferences.Editor editor=sharedPreferences.edit();
- editor.putString("etName","Name");
- editor.putString("etPass","pass");
- editor.putBoolean("checkboxBoolean",true);
- editor.commit();
- }
- else{
- SharedPreferences.Editor editor=sharedPreferences.edit();
- editor.putString("etName","");
- editor.putString("etPass","");
- editor.putBoolean("checkboxBoolean",false);
- editor.commit();
- }
- Intent intent=new Intent(LoginActivity.this,ContentActivity.class);
- startActivity(intent);
- finish();
- }else{
- Toast.makeText("this","账号或者密码有误",Toast.LENGTH_LONG);
- }
- }
- }
[html] view plain copy
- package com.example.bz0209.login;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- public class ContentActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_content);
- }
- }
0 0
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录(记住密码)
- 用户登录记住密码
- [meet in middle 矩阵树定理 容斥原理] SRM 551 div1 SweetFruits
- L1-032. Left-pad java
- 递归递推 E
- IntelliJ Idea 常用快捷键列表
- LintCode 把排序数组转换为高度最小的二叉搜索树
- 用户登录记住密码
- ZUNIONSTORE
- FZU2092-收集水晶
- LintCode 子树
- javascript继承
- Android GPS定位(获取经纬度)
- java多线程:17、面试题1
- python2 与Python 3 的区别(持续更新...)
- Android RadioButton代码去掉圆点绝对有效