Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the small function of remembering passwords in Android

Shulou Source: shulou.com Published: 2022-06-03 10:47:58 10月03日 Update

This article mainly introduces Android how to achieve the small function of remembering passwords, which is very detailed and has a certain reference value. Friends who are interested must read it!

The details are as follows

There are three points below: the first point is to remember the password, the second point is to click hide and click display, and the third point is to log in and store.

XML layout

Java code

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.activitymains;} @ Override protected LoginPresenter CreatPresenter () {return new LoginPresenter ();} @ Override protected void loadData () {ButterKnife.bind (this); / / Hidden mima.setTransformationMethod (PasswordTransformationMethod.getInstance ()) by default / Click the small eye 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 ()); / / display sb=false } else {can.setImageResource (R.mipmap.login_icon_hide_password_n); mima.setTransformationMethod (PasswordTransformationMethod.getInstance ()); / / hide sb=true;}); / / remember the password 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) } / / Click to log in to deng.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {String S1 = youxiang.getText () .toString (); / / get the input box mailbox String S2 = mima.getText () .toString () / / get the input box password String a = ""; / / store the encrypted password try {a = RsaCoder.encryptByPublicKey (S2); / / encrypted password} catch (Exception e) {e.printStackTrace () } if (s1.equals (")) {/ / if the mailbox is empty, toast Toast.makeText (MainActivity.this," Please enter mailbox ", Toast.LENGTH_SHORT) .show () } else if (s2.equals (")) {/ / if the password is empty, toast Toast.makeText (MainActivity.this," Please enter password ", Toast.LENGTH_SHORT). Show ();} else {/ / this is the P-layer mPresenter.ShowDengPresenter called by MVP (S1 mPresenter.ShowDengPresenter) / / S1 is the mailbox an is the encrypted password / / remember the password edit = user.edit (); if (ji.isChecked ()) {edit.putBoolean ("remember_password", true); edit.putString ("phone", S1) / / No encrypted mailbox edit.putString ("password", S2); / / No encrypted password} 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 () / / after success, jump to the home page Intent intent = new Intent (MainActivity.this,HomeActivity.class); startActivity (intent); finish ();} above is all the content of the article "how to remember passwords in Android". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Password mailbox encryption input content function toast article login success three under code value interest buddy partner layout more eyes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Microsoft Shulou Technology Linux MySQL