溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在Android應(yīng)用中實(shí)現(xiàn)一個(gè)記住密碼功能

發(fā)布時(shí)間:2020-12-05 15:18:42 來源:億速云 閱讀:385 作者:Leah 欄目:移動開發(fā)

本篇文章給大家分享的是有關(guān)如何在Android應(yīng)用中實(shí)現(xiàn)一個(gè)記住密碼功能,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

一、打開之前完成的Case_login進(jìn)行修改再編輯

如何在Android應(yīng)用中實(shí)現(xiàn)一個(gè)記住密碼功能

二、將注冊按鈕刪除并在登錄按鈕前拖出一個(gè)checkBox,編輯代碼如下:

在layout_top.xml文件中進(jìn)行編輯

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<RelativeLayout xmlns:android="http://schemas.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應(yīng)用中實(shí)現(xiàn)一個(gè)記住密碼功能

三、對登錄密碼及記住密碼進(jìn)行編輯

在LoginAcitvity.java文件進(jìn)行編寫修改,代碼如下:

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

 }

}

四、設(shè)計(jì)登錄后的界面

新建一個(gè)MainActivity.java文件,同時(shí)生成一個(gè)activity_main.xml,對其進(jìn)行編寫代碼效果圖如下:

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.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應(yīng)用中實(shí)現(xiàn)一個(gè)記住密碼功能

以上就是如何在Android應(yīng)用中實(shí)現(xiàn)一個(gè)記住密碼功能,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI