Android实现记住密码小功能

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

Android实现记住密码小功能_第1张图片

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

XML布局




    
    


    

      
      

      
      


    


  

    
    

    
    

    
    
  


  
  


  

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 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();
        }

    }
}

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

你可能感兴趣的:(Android实现记住密码小功能)