Android實現記住密碼小功能

本文實例為大傢分享瞭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();
        }

    }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: