用户登录记住密码

来源:互联网 发布:数据统计与分析专业 编辑:程序博客网 时间:2024/05/01 18:28

    我下面介绍的是记住密码的用户登录,以下就是此实验:

一、实验原理

  实现用户名和密码的功能是通过SharedPreferences存储实现的。当用户选中了记住密码复选框,并成功登录一次后,这个时候如果再重新启动登录界面。之前输入的用户名密码就会显示在文本框中,就会被保存,下次登录就不用再用密码

二、训练项目

  掌握SharedPreferences的使用。

三、案例实现

  登录对应的布局文件Activity_login.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/activity_login"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     android:background="#ADD8E6"  
  12.     android:orientation="vertical"  
  13.     tools:context="com.example.bz0209.login.LoginActivity">  
  14.     <RelativeLayout  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="200dp"  
  17.         android:background="@drawable/toproundbg"  
  18.         android:padding="@dimen/activity_horizontal_margin"  
  19.         android:id="@+id/shitu">  
  20.         <EditText  
  21.             android:id="@+id/etName"  
  22.             android:layout_width="match_parent"  
  23.             android:layout_height="wrap_content"  
  24.             android:drawableLeft="@drawable/icon_user"  
  25.             android:ems="10"  
  26.             android:hint="输入账号:"  
  27.             android:drawablePadding="10dp"/>  
  28.         <requestFocus/>  
  29.         <EditText  
  30.             android:id="@+id/etPass"  
  31.             android:layout_width="match_parent"  
  32.             android:layout_height="wrap_content"  
  33.             android:layout_below="@+id/etName"  
  34.             android:drawableLeft="@drawable/icon_pass"  
  35.             android:drawablePadding="10dp"  
  36.             android:ems="10"  
  37.             android:hint="输入密码:"/>  
  38.         <requestFocus/>  
  39.         <LinearLayout  
  40.             android:layout_width="wrap_content"  
  41.             android:layout_height="wrap_content"  
  42.             android:layout_below="@+id/etPass"  
  43.             android:layout_marginTop="10dp"  
  44.             >  
  45.             <CheckBox  
  46.                 android:layout_marginTop="10dp"  
  47.                 android:id="@+id/etCheckBox"  
  48.                 android:layout_width="wrap_content"  
  49.                 android:layout_height="wrap_content"  
  50.                 android:text="记住密码"  
  51.                 />  
  52.             <Button  
  53.                 android:id="@+id/etLogin"  
  54.                 android:layout_marginLeft="90dp"  
  55.                 android:layout_width="wrap_content"  
  56.                 android:layout_height="wrap_content"  
  57.                 android:background="@drawable/btn_shape"  
  58.                 android:onClick="btnClick"  
  59.                 android:text="登录"/>  
  60.         </LinearLayout>  
  61.   
  62.     </RelativeLayout>  
  63.     <ImageView  
  64.         android:layout_width="wrap_content"  
  65.         android:layout_height="wrap_content"  
  66.         android:layout_marginTop="100dp"  
  67.         android:layout_marginLeft="100dp"  
  68.         android:background="@drawable/deer"  
  69.         />  
  70. </LinearLayout>  
Activity_content.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/activity_content"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     tools:context="com.example.bz0209.login.ContentActivity">  
  12.     <TextView  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="内容"  
  16.         android:textSize="30sp"  
  17.         android:textColor="@color/colorAccent"/>  
  18. </RelativeLayout>  
[html] view plain copy
  1. <span style="font-size:18px;">界面交互界面LoginActivity</span>  
[html] view plain copy
  1. package com.example.bz0209.login;  
  2.   
  3. import android.content.DialogInterface;  
  4. import android.content.Intent;  
  5. import android.content.SharedPreferences;  
  6. import android.support.v7.app.AppCompatActivity;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.CheckBox;  
  11. import android.widget.EditText;  
  12. import android.widget.Toast;  
  13.   
  14. public class LoginActivity extends AppCompatActivity implements View.OnClickListener{  
  15.     private EditText etName=null;  
  16.     private  EditText etPass=null;  
  17.     private Button etLogin=null;  
  18.     private CheckBox etCheckBox=null;  
  19.     SharedPreferences sharedPreferences=null;  
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_login);  
  24.         sharedPreferences=getSharedPreferences("userInfo",MODE_PRIVATE);  
  25.         initView();  
  26.     }  
  27.   
  28.   
  29.     private void initView() {  
  30.         etName=(EditText) findViewById(R.id.etName);  
  31.         etPass=(EditText)findViewById(R.id.etPass);  
  32.         etLogin=(Button)findViewById(R.id.etLogin);  
  33.         etCheckBox=(CheckBox)findViewById(R.id.etCheckBox);  
  34.         if(sharedPreferences.getBoolean("checkboxBoolean",false)){  
  35.             etName.setText(sharedPreferences.getString("etName",""));  
  36.             etPass.setText(sharedPreferences.getString("etPass",""));  
  37.             etCheckBox.setChecked(true);  
  38.         }  
  39.        findViewById(R.id.etLogin).setOnClickListener(this);  
  40.     }  
  41.      public void onClick(View view){  
  42.          String Name=etName.getText().toString();  
  43.          String Pass=etPass.getText().toString();  
  44.          if(Name.isEmpty()){  
  45.              Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT);  
  46.              return;  
  47.          }  
  48.          if(Pass.isEmpty()){  
  49.              Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT);  
  50.              return;  
  51.          }  
  52.          if("gls".equals(Name)&&"123".equals(Pass){  
  53.            boolean CheckLogin=etCheckBox.isChecked();  
  54.            if(CheckLogin){  
  55.              SharedPreferences.Editor editor=sharedPreferences.edit();  
  56.              editor.putString("etName","Name");  
  57.              editor.putString("etPass","pass");  
  58.              editor.putBoolean("checkboxBoolean",true);  
  59.              editor.commit();  
  60.            }  
  61.            else{  
  62.              SharedPreferences.Editor editor=sharedPreferences.edit();  
  63.              editor.putString("etName","");  
  64.              editor.putString("etPass","");  
  65.              editor.putBoolean("checkboxBoolean",false);  
  66.              editor.commit();  
  67.            }  
  68.          Intent intent=new Intent(LoginActivity.this,ContentActivity.class);  
  69.          startActivity(intent);  
  70.          finish();  
  71.          }else{  
  72.             Toast.makeText("this","账号或者密码有误",Toast.LENGTH_LONG);  
  73.          }  
  74.      }  
  75. }  
跳转界面MainActivity

[html] view plain copy
  1. package com.example.bz0209.login;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5.   
  6. public class ContentActivity extends AppCompatActivity {  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_content);  
  12.     }  
  13. }  
四、最终结果





0 0
原创粉丝点击