Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android实现用户登录记住密码功能大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

Android实现用户登录记住密码功能

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

在layout_top.xml文件中进行编辑

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://scheR_877_11845@as.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();
  }

 }

}

四、设计登录后的界面

新建@L_717_4@mainActivity.java文件,同时@L_262_20@一个activity_main.xml,对其进行编写代码效果图如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://scheR_877_11845@as.android.com/apk/res/android"
 xmlns:tools="http://scheR_877_11845@as.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实现用户登录记住密码功能

Android实现用户登录记住密码功能

再次打开软件密码就记住了如图:

Android实现用户登录记住密码功能


输入错误的密码有提示如下图:

Android实现用户登录记住密码功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

大佬总结

以上是大佬教程为你收集整理的Android实现用户登录记住密码功能全部内容,希望文章能够帮你解决Android实现用户登录记住密码功能所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。