Android实现记住密码小功能

编辑: admin 分类: Android 发布时间: 2021-11-29 来源:互联网

本文实例为大家分享了Android实现记住密码小功能的具体代码,供大家参考,具体内容如下

以下有三个点 第一点是记住密码, 第二点是点击隐藏点击显示, 第三点是登录存储。

XML布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".v.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textSize="20sp"
        android:textColor="#FFEB3B"
        android:gravity="center"
        android:padding="10dp"
        android:background="#8BC34A"
        >
    </TextView>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        >

      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="邮箱:"
          android:textColor="#03A9F4"
          android:textSize="15sp"
          android:textStyle="italic"
          android:layout_marginLeft="30dp"
          android:padding="10dp"
          >
      </TextView>

      <EditText
          android:id="@+id/youxiang"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:hint="请输入邮箱"
          android:paddingLeft="10dp"
          android:textColorHint="#FF5722"
          android:textStyle="italic"
          android:layout_marginRight="40dp"
          >
      </EditText>


    </LinearLayout>


  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:layout_marginTop="10dp"
      >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码:"
        android:textColor="#03A9F4"
        android:textSize="15sp"
        android:textStyle="italic"
        android:layout_marginLeft="30dp"
        android:padding="10dp"
        >
    </TextView>

    <EditText
        android:id="@+id/mima"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:paddingLeft="10dp"
        android:textColorHint="#FF5722"
        android:textStyle="italic"
        >
    </EditText>

    <ImageView
        android:id="@+id/can"
        android:layout_width="20dp"
        android:layout_height="match_parent"
        android:src="@mipmap/login_icon_hide_password_n"
        android:layout_marginRight="20dp"
        >
    </ImageView>
  </LinearLayout>


  <CheckBox
      android:id="@+id/ji"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="记住密码"
      android:textColor="#FF5722"
      android:layout_marginLeft="40dp"
      android:layout_marginTop="15dp"
      >
  </CheckBox>


  <Button
      android:id="@+id/deng"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="登录"
      android:textColor="#FF5722"
      android:background="#03A9F4"
      android:textStyle="bold"
      android:textSize="15sp"
      android:layout_margin="30dp"
      >
  </Button>
</LinearLayout>

Java代码

package com.wd.health.v;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.wd.health.R;
import com.wd.health.base.BaseActivity;
import com.wd.health.bean.LoginBean;
import com.wd.health.contract.ILoginContract;
import com.wd.health.net.RsaCoder;
import com.wd.health.p.LoginPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity<LoginPresenter> implements ILoginContract.ILoginView {

    @BindView(R.id.can)
    ImageView can;
    @BindView(R.id.ji)
    CheckBox ji;
    @BindView(R.id.deng)
    Button deng;
    @BindView(R.id.youxiang)
    EditText youxiang;
    @BindView(R.id.mima)
    EditText mima;
    boolean sb=true;
    private SharedPreferences user;
    private SharedPreferences.Editor edit;

    @Override
    protected int initView() {
        return R.layout.activity_main;
    }

    @Override
    protected LoginPresenter CreatPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void loadData() {
        ButterKnife.bind(this);
        //默认是隐藏
        mima.setTransformationMethod(PasswordTransformationMethod.getInstance());
        //点击小眼睛
        can.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sb){
                    can.setImageResource(R.mipmap.login_icon_show_password);
                    mima.setTransformationMethod(HideReturnsTransformationMethod.getInstance());    //显示
                    sb=false;
                }else {
                    can.setImageResource(R.mipmap.login_icon_hide_password_n);
                    mima.setTransformationMethod(PasswordTransformationMethod.getInstance());   //隐藏
                    sb=true;
                }
            }
        });
        //记住密码
        user = getSharedPreferences("user", MODE_PRIVATE);
        boolean isRemenber = user.getBoolean("remember_password", false);
        if(isRemenber){
            youxiang.setText(user.getString("phone",""));
            mima.setText(user.getString("password",""));
            ji.setChecked(true);
        }

        //点击登录
        deng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s1 = youxiang.getText().toString();      //获取输入框邮箱
                String s2 = mima.getText().toString();          //获取输入框密码
                String a="";        //存放加密的密码
                try {
                    a = RsaCoder.encryptByPublicKey(s2);        //加密后的密码
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (s1.equals("")){         //如果邮箱为空则吐司
                    Toast.makeText(MainActivity.this, "请输入邮箱", Toast.LENGTH_SHORT).show();
                }else if (s2.equals("")){   //如果密码为空则吐司
                    Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                }else {
                    //这是MVP调用的P层
                    mPresenter.ShowDengPresenter(s1,a);     //s1是邮箱  a是加密的密码
                    //记住密码
                    edit = user.edit();
                    if(ji.isChecked()){
                        edit.putBoolean("remember_password",true);
                        edit.putString("phone",s1);        //没有加密的邮箱
                        edit.putString("password",s2);      //没有加密的密码
                    }else{
                        edit.clear();
                    }
                    edit.apply();
                }

            }
        });

    }

    @Override
    public void ShowDengView(LoginBean loginBean) {
        Toast.makeText(this, loginBean.getMessage(), Toast.LENGTH_SHORT).show();
        if (loginBean.getStatus().equals("0000")){
            user.edit().putString("userId",String.valueOf(loginBean.getResult().getUserId()))
                    .putString("sessionId",loginBean.getResult().getSessionId())
                    .putString("nickName",loginBean.getResult().getNickName())
                    .putString("userName",loginBean.getResult().getUserName())
                    .putString("jiGuangPwd",loginBean.getResult().getJiGuangPwd())
                    .putString("headPic",loginBean.getResult().getHeadPic())
                    .putString("sex",String.valueOf(loginBean.getResult().getSex()))
                    .putString("age",String.valueOf(loginBean.getResult().getAge()))
                    .putString("height",String.valueOf(loginBean.getResult().getHeight()))
                    .putString("weight",String.valueOf(loginBean.getResult().getWeight()))
                    .putString("email",String.valueOf(loginBean.getResult().getEmail()))
                    .putString("whetherBingWeChat",String.valueOf(loginBean.getResult().getWhetherBingWeChat()))
                    .putString("invitationCode",String.valueOf(loginBean.getResult().getInvitationCode()))
                    .putString("faceFlag",String.valueOf(loginBean.getResult().getFaceFlag()))
                    .commit();
            //成功后跳转到首页
            Intent intent = new Intent(MainActivity.this,HomeActivity.class);
            startActivity(intent);
            finish();
        }

    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持海外IDC网。

【来源:海外服务器https://www.68idc.cn】