Android登录、注册界面,并使用SharedPreferences进行数据的存储

本篇文章主要是制作登录界面与注册界面,并使用Android中用于存储轻量级数据的SharedPreferences存储用户输入的数据

这里就不介绍SharedPreferences的使用了,不懂的可以搜索SharedPreferences的使用

登录布局文件 login_activity_layout.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"
    >

    <!--<ImageView
        android:layout_width="match_parent"
        android:scaleType="fitXY"
        android:layout_height="200dp"
        android:src="@drawable/bg01"
        />-->

    <TextView
        android:layout_margin="20dp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="25sp"
        android:textColor="#FF9800"
        android:text="登 录 界 面"
        />

    <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <!-- 账号 -->
        <LinearLayout
            android:background="@drawable/login_et_bg"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            >

            <ImageView
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/baseline_account_box_24"/>
            <EditText
                android:id="@+id/et_acount"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="10dp"
                android:background="@null"
                android:textSize="14sp"
                android:hint="请输入用户名"
                />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"/>

        <!-- 密码 -->
        <LinearLayout
            android:background="@drawable/login_et_bg"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            >

            <ImageView
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/baseline_lock_24"/>
            <EditText
                android:id="@+id/et_pwd"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="10dp"
                android:background="@null"
                android:textSize="14sp"
                android:inputType="textPassword"
                android:hint="请输入密码"
                />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"/>

        <TextView
            android:id="@+id/tv_register"
            android:layout_margin="10dp"
            android:layout_gravity="right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FF9800"
            android:text="还未注册?"
            />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_login"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@drawable/login_btn"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="@color/white"
            android:text="登 录"
            />


    </LinearLayout>



</LinearLayout>

图标(登陆注册界面都是这两个图标)

baseline_account_box_24.xml
<vector android:height="24dp" android:tint="#EB9656"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="@android:color/white" android:pathData="M3,5v14c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2L5,3c-1.11,0 -2,0.9 -2,2zM15,9c0,1.66 -1.34,3 -3,3s-3,-1.34 -3,-3 1.34,-3 3,-3 3,1.34 3,3zM6,17c0,-2 4,-3.1 6,-3.1s6,1.1 6,3.1v1L6,18v-1z"/>
</vector>
baseline_lock_24.xml
<vector android:height="24dp" android:tint="#EB9656"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="@android:color/white" android:pathData="M18,8h-1L17,6c0,-2.76 -2.24,-5 -5,-5S7,3.24 7,6v2L6,8c-1.1,0 -2,0.9 -2,2v10c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,10c0,-1.1 -0.9,-2 -2,-2zM12,17c-1.1,0 -2,-0.9 -2,-2s0.9,-2 2,-2 2,0.9 2,2 -0.9,2 -2,2zM15.1,8L8.9,8L8.9,6c0,-1.71 1.39,-3.1 3.1,-3.1 1.71,0 3.1,1.39 3.1,3.1v2z"/>
</vector>
LinearLayout样式login_et_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--    背景颜色-->
    <solid android:color="@color/white" />
    <!--    圆角(上下左右)-->
    <corners android:radius="25dp" />
    <!--    边框颜色和宽度-->
    <stroke
        android:width="1dp"
        android:color="#f5f5f5" />

</shape>

登录代码 LoginActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_acount;
    private EditText et_pwd;
    private SharedPreferences sp;
    private Button btn_login;
    private TextView tv_register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        ViewClick();

    }


    /**
     * 初始化控件
     */
    public void initView(){
        et_acount = findViewById(R.id.et_acount);
        et_pwd = findViewById(R.id.et_pwd);
        btn_login = findViewById(R.id.btn_login);
        tv_register = findViewById(R.id.tv_register);
        sp = getSharedPreferences("userinfo", MODE_PRIVATE);
    }

    /**
     * 设置控件的点击事件
     */
    public void ViewClick(){
        btn_login.setOnClickListener(this);
        tv_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_login){

            // 获取用户输入的数据
            String etAcount = et_acount.getText().toString();
            String etPwd = et_pwd.getText().toString();

            // 点击登录,获取SharedPreferences里存储的数据(即注册里创建的共享文件)
            String acount = sp.getString("acount", "");
            String pwd = sp.getString("pwd", "");

            // 判断用户输入的数据与SharedPreferences里存储的数据是否一致
            if(acount.equals(etAcount) && pwd.equals(etPwd)){
                // 满足条件,登录成功
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
            }else {
                // 不满足条件
                Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
            }

        } else if (v.getId() == R.id.tv_register) {
            // 跳转注册页面
            startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
            // 清空输入框
            et_pwd.setText("");
            et_acount.setText("");
        }
    }
}

登录页面预览

06ae6f7b1dc94cbb80e1b6ab969efaff.png

 

注册界面布局文件 register_activity_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/img_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="10dp"
            android:src="@drawable/left" />

    </RelativeLayout>

    <TextView
        android:layout_margin="10dp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="25sp"
        android:textColor="#FF9800"
        android:text="注 册 界 面"
        />

    <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <!-- 账号 -->
        <LinearLayout
            android:background="@drawable/login_et_bg"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            >

            <ImageView
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/baseline_account_box_24"/>
            <EditText
                android:id="@+id/et_acount"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="10dp"
                android:background="@null"
                android:textSize="14sp"
                android:hint="请输入用户名"
                />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"/>

        <!-- 密码 -->
        <LinearLayout
            android:background="@drawable/login_et_bg"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            >

            <ImageView
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/baseline_lock_24"/>
            <EditText
                android:id="@+id/et_pwd"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="10dp"
                android:background="@null"
                android:textSize="14sp"
                android:inputType="textPassword"
                android:hint="请输入密码"
                />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"/>

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_register"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@drawable/login_btn"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="@color/white"
            android:text="注 册"
            />


    </LinearLayout>



</LinearLayout>

注册代码 RegisterActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_acount;
    private EditText et_pwd;
    private SharedPreferences sp;
    private Button btn_register;
    private ImageView img_left;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        setSharedPreferences();
        initView();
        ViewClick();

    }

    /**
     * 创建数据存储文件
     */
    public void setSharedPreferences() {
        sp = getSharedPreferences("userinfo", MODE_PRIVATE);
    }

    /**
     * 初始化控件
     */
    public void initView() {
        et_acount = findViewById(R.id.et_acount);
        et_pwd = findViewById(R.id.et_pwd);
        btn_register = findViewById(R.id.btn_register);
        img_left = findViewById(R.id.img_left);
    }

    /**
     * 设置控件的点击事件
     */
    public void ViewClick() {
        btn_register.setOnClickListener(this);
        img_left.setOnClickListener(this);
    }

    /**
     * 校验密码
     */
    public boolean verityPassword(String pwd) {
        boolean matches = pwd.matches("[1-9]\\d{5,19}");
        if (!matches) {
            Toast.makeText(RegisterActivity.this, "密码长度应在6~20之间", Toast.LENGTH_SHORT).show();
            return false;
        } else {
            return true;
        }
    }


    public void onClick(View v) {
        if (v.getId() == R.id.btn_register) {
            String acount = et_acount.getText().toString();
            String pwd = et_pwd.getText().toString();
            // 判断账号和密码是否为空
            if (!TextUtils.isEmpty(acount) && !TextUtils.isEmpty(pwd)) {
                // 调用检验密码函数,进行密码的校验
                boolean flag = verityPassword(pwd);
                if (flag) {
                    Toast.makeText(RegisterActivity.this, "注册成功,请返回登录!", Toast.LENGTH_SHORT).show();
                    // 将账号和密码存起来
                    sp.edit().putString("acount",acount).apply();
                    sp.edit().putString("pwd",pwd).apply();
                }
            }else{
                Toast.makeText(RegisterActivity.this, "账号和密码不能为空", Toast.LENGTH_SHORT).show();
            }


        } else if (v.getId() == R.id.img_left) {
            // 结束活动
            finish();
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        super.onPointerCaptureChanged(hasCapture);
    }
}

注册页面预览

2b6386cf23f54f0cb7a894e76dff4b02.png

至此整个简单的登录、注册制作与使用SharedPreferences进行数据的存储就完成了,还有很多地方可优化,美中不足的地方,各位大佬可以在评论区探讨探讨,给一点点建议,希望这个小小的分享能帮到你们,带给你灵感,我们共同进步。

 

  • 24
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现 Android Studio 账户密码验证登录功能可以使用 SharedPreferences(共享偏好设置)类。下面是一个简单的示例代码: 1. 在 LoginActivity 中添加以下代码: ```java public class LoginActivity extends AppCompatActivity { private EditText mUsernameEditText; private EditText mPasswordEditText; private Button mLoginButton; private SharedPreferences mSharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mSharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE); mUsernameEditText = findViewById(R.id.username_edit_text); mPasswordEditText = findViewById(R.id.password_edit_text); mLoginButton = findViewById(R.id.login_button); mLoginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String username = mUsernameEditText.getText().toString(); String password = mPasswordEditText.getText().toString(); // 获取保存的用户名和密码 String savedUsername = mSharedPreferences.getString("username", ""); String savedPassword = mSharedPreferences.getString("password", ""); if (username.equals(savedUsername) && password.equals(savedPassword)) { // 登录成功 Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); // 跳转到主界面 Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); } else { // 登录失败 Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show(); } } }); } } ``` 在这个示例中,我们通过 `getSharedPreferences()` 方法获取一个 SharedPreferences 对象,并使用 `getString()` 方法获取保存的用户名和密码,然后与用户输入的用户名和密码进行比较,如果相等则登录成功,否则登录失败。 2. 在 MainActivity 中添加以下代码: ```java public class MainActivity extends AppCompatActivity { private Button mLogoutButton; private SharedPreferences mSharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE); mLogoutButton = findViewById(R.id.logout_button); mLogoutButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 清除保存的用户名和密码 SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.remove("username"); editor.remove("password"); editor.apply(); // 跳转到登录界面 Intent intent = new Intent(MainActivity.this, LoginActivity.class); startActivity(intent); finish(); } }); } } ``` 在这个示例中,我们使用 `getSharedPreferences()` 方法获取一个 SharedPreferences 对象,并使用 `remove()` 方法清除保存的用户名和密码,然后跳转到登录界面。 需要注意的是,这只是一个简单的示例代码,实际应用中还需要进行更多的错误处理和安全性检查。同时,使用 SharedPreferences 存储敏感数据并不是最安全的方式,建议使用其他更加安全的存储方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值