Android 实现简单的账号密码登陆

147 阅读1分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第20天,点击查看活动详情

老规矩,咱们先上效果图如下:

页面代码如下:

制作一个简单的登录界面,选中记住账号密码,在下次打开应用时自动获取上次储存的账户和密码

<?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=".MainActivity">

    <EditText
        android:id="@+id/et_zh"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:hint="账号"
        />

    <EditText
        android:id="@+id/et_mm"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:hint="密码"
        />
    <CheckBox
        android:id="@+id/ck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登陆"
        />


</LinearLayout>
  • 处理如下:

判断账号和密码是是否一样,不一样toast提示密码账户不一样,否则跳转到下一个页面,数据是用SharedPreferences传递数据

package com.example.loginactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText etZh,etMm;
    private Button btn;
    private CheckBox ck;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();//Alt+回车
        SharedPreferences sp=getSharedPreferences("zhmm",0);
//        tv1.setText(sp.getString("zhkey",""));
//        tv2.setText(sp.getString("mmkey",""));
         boolean ckkey=sp.getBoolean("ckkey",false);
         String zhkey=sp.getString("zhkey","");
         String mmkey=sp.getString("mmkey","");

         if(ckkey){//记住密码
             etZh.setText(zhkey);
             etMm.setText(mmkey);
         }else {
             etZh.setText("");
             etMm.setText("");
         }

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //处理点击事件
               String zh1= etZh.getText().toString();//输入的账号
               String mm1= etMm.getText().toString();//输入的密码
               if(zh1.equals(mm1)){
                   Intent intent=new Intent(MainActivity.this,Main3Activity.class);
                   intent.putExtra("zh1",zh1);
                   intent.putExtra("mm1",mm1);
                   startActivity(intent);
                   //如果选中记住密码
                   if (ck.isChecked()){//去存账号密码
                       SharedPreferences sp=getSharedPreferences("zhmm",0);//alt+回车
                       final SharedPreferences.Editor edit = sp.edit();
                       edit.putString("zhkey",zh1);
                       edit.putString("mmkey",mm1);
                       edit.putBoolean("ckkey",true);
                       edit.apply();
                   }
               }else {
                   Toast.makeText(MainActivity.this,"账号密码不一致",Toast.LENGTH_LONG).show();
               }
            }
        });

    }

    private void initView() {
        btn=findViewById(R.id.btn);
        etMm= findViewById(R.id.et_mm);
        etZh=findViewById(R.id.et_zh);
        ck=findViewById(R.id.ck);
    }
}