如何在Android应用中实现一个记住密码功能

发布时间:2020-12-05 15:18:42 作者:Leah
来源:亿速云 阅读:368

本篇文章给大家分享的是有关如何在Android应用中实现一个记住密码功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

一、打开之前完成的Case_login进行修改再编辑

如何在Android应用中实现一个记住密码功能

二、将注册按钮删除并在登录按钮前拖出一个checkBox,编辑代码如下:

在layout_top.xml文件中进行编辑

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:padding="@dimen/activity_horizontal_margin"
 android:background="@drawable/logintop_roundbg">

 <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">

  <Button
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:background="@drawable/btn_select"
   android:text="@string/btnLogin" />

  <Button
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:layout_marginLeft="10dp"
   android:background="@drawable/btn_select"
   android:text="@string/btnRegister" />

 </LinearLayout>
</RelativeLayout>

效果图如下:

如何在Android应用中实现一个记住密码功能

三、对登录密码及记住密码进行编辑

在LoginAcitvity.java文件进行编写修改,代码如下:

package cn.edu.bzu.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 cbIsRememberPass;
 private SharedPreferences sharedPreferences;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_login);
  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);


  }

 }
 private 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,MainActivity.class);
   startActivity(intent);
   finish();
  }
  else {
   Toast.makeText(this,"账户或密码错误",Toast.LENGTH_LONG).show();
  }

 }

}

四、设计登录后的界面

新建一个MainActivity.java文件,同时生成一个activity_main.xml,对其进行编写代码效果图如下:

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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.edu.bzu.case_login.MainActivity">

 <TextView
  android:text="Welcome you"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerVertical="true"
  android:layout_centerHorizontal="true"
  android:textSize="40sp"
  android:id="@+id/textView" />
</RelativeLayout>

如何在Android应用中实现一个记住密码功能

以上就是如何在Android应用中实现一个记住密码功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. SharedPreferences临时存储数据 如记住密码
  2. 如何在Android应用中实现一个图片添加功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android roi

上一篇:Android应用中怎么将Drawable转换成Bitmap

下一篇:Android应用中怎么实现一个滑动切换页面功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》