当前位置: 首页 > article >正文

基于Android Studio购物商城app+web端,登录实现(前后端分离)二

文章目录

    • 1. 涉及到技术点
    • 2. 服务端编写登录接口
    • 3. 编写登录UI界面
    • 4. 记住密码实现

1. 涉及到技术点

  1. 使用MySQL实现数据储存
  2. SharedPreferences首选项的使用
  3. LinearLayoutCompat线性布局的使用
  4. ImageView,TextView,EditText,Button,CheckBox基础控件的使用

2. 服务端编写登录接口

这里忽略如何在服务端编写登录接口,本项目是前后端分离实现,app端的数据交互都是调用服务端接口来实现数据交互

3. 编写登录UI界面

  1. activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">


    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/my_light_primary"
            app:title="登录"
            app:titleTextColor="@color/white">


            <ImageView
                android:id="@+id/btn_login_close"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="right"
                android:padding="10dp"
                android:src="@drawable/baseline_close_24" />
        </androidx.appcompat.widget.Toolbar>


        <androidx.cardview.widget.CardView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="50dp"
            app:cardCornerRadius="50dp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@mipmap/img_logo" />


        </androidx.cardview.widget.CardView>


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


        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="26dp"
            android:orientation="vertical">


            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@drawable/login_et_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:src="@drawable/baseline_account_box_24" />

                <EditText
                    android:id="@+id/et_username"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
                    android:background="@null"
                    android:hint="请输入用户名"
                    android:textSize="14sp" />


            </androidx.appcompat.widget.LinearLayoutCompat>


            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/login_et_bg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:src="@drawable/baseline_https_24" />

                <EditText
                    android:id="@+id/et_password"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
                    android:background="@null"
                    android:hint="请输入密码"
                    android:inputType="textPassword"
                    android:textSize="14sp" />


            </androidx.appcompat.widget.LinearLayoutCompat>


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


                <CheckBox
                    android:id="@+id/checkbox"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="记住密码" />

                <TextView
                    android:id="@+id/register"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_gravity="right"
                    android:layout_marginTop="10dp"
                    android:text="还未注册?"
                    android:textColor="@color/my_light_primary" />

            </RelativeLayout>


            <Button
                android:id="@+id/login"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="20dp"
                android:text="登录" />


        </androidx.appcompat.widget.LinearLayoutCompat>


    </androidx.appcompat.widget.LinearLayoutCompat>

</RelativeLayout>

运行效果图

在这里插入图片描述

  1. LoginActivity.java 具体逻辑实现

a. 初始化控件

        //初始化控件
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        checkbox = findViewById(R.id.checkbox);
        btn_login_close = findViewById(R.id.btn_login_close);

b. 设置登录按钮的点击事件

 //登录点击事件
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = et_username.getText().toString();
                String password = et_password.getText().toString();
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    showToast("请输入用户名和密码");
                } else {
                    login(username, password);
                }
            }
        });

c. 调用接口获取登录信息

    /**
     * 登录
     */
    private void login(String username, String password) {
        OkGo.<String>get(ApiConstants.LOGIN_URL)
                .params("username", username)
                .params("password", password)
                .execute(new HttpStringCallback(mBaseActivity) {
                    @Override
                    protected void onSuccess(String msg, String response) {
                        showToast(msg);
                        UserInfo userInfo = GsonUtils.parseJson(response, UserInfo.class);
                        //保存用户名和密码
                        SharedPreferences.Editor edit = mSharedPreferences.edit();
                        edit.putBoolean("is_login", is_login);
                        edit.putString("username", username);
                        edit.putString("password", password);
                        //这句话不能少
                        edit.commit();

                        UserInfo.setUserInfo(userInfo);
                        finish();

                    }

                    @Override
                    protected void onError(String response) {
                        showToast(response);
                    }
                });


    }

温馨提示:调用接口API,可以使用开源网络库OkHttp来实现,本项目中使用OkGo开源库来实现,因为OkGo封装了OkHttp,简化了OkHttp的使用。OkGo可通过链式调用的形式,可以很简单的实现接口API数据的获取。

4. 记住密码实现

记住密码使用SharedPreferences来实现,实现思路: 用到SharedPreferences就需要先实例化,然后在调用登录接口成功后,保存用户名,密码和 是否记住密码状态,最后根据否记住密码状态来判断显示在控件上面

  1. 初始化SharedPreferences
        //获取mSharedPreferences实例
        mSharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
  1. 在登录成功后,保存用户名和密码
       //保存用户名和密码
      SharedPreferences.Editor edit = mSharedPreferences.edit();
      edit.putBoolean("is_login", is_login);
      edit.putString("username", username);
      edit.putString("password", password);
       //这句话不能少
      edit.commit();
  1. 根据否记住密码状态来判断显示在控件上面
        //是否勾选了记住密码
        is_login = mSharedPreferences.getBoolean("is_login", false);
        if (is_login) {
            String username = mSharedPreferences.getString("username", null);
            String password = mSharedPreferences.getString("password", null);
            et_username.setText(username);
            et_password.setText(password);
            checkbox.setChecked(true);

        }

http://www.kler.cn/news/359481.html

相关文章:

  • ant design vue TimePicker时间选择器不点击确认也可以设置值
  • 破局汽车基础软件发展丨昂辉科技亮相2024芜湖新能源汽车零部件和后市场生态博览会
  • 【C++STL】list的基本介绍与使用方式
  • Django学习-静态文件
  • 【机器学习】简单易懂的聚类算法K-Means
  • 每日回顾:简单用C写 选择排序、堆排序
  • 基于Android Studio购物商城app+web端实现(前后端分离)一
  • Thread类的基本用用法
  • 基于Multisim旗升降自动控制系统电路(含仿真和报告)
  • python全栈开发《47.索引与切片之字符串》
  • Django-应用及分布式路由
  • 深入解析JavaScript中的箭头函数及其在React中的应用(箭头函数与传统函数的区别、如何在不同上下文中使用箭头函数)
  • 【前端】如何制作自己的网站(7)
  • echarts设置x轴中文垂直显示,x轴滚动条
  • 随机数生成
  • React 学习计划
  • Modelsim:LPDDR5仿真(含美光仿真模型官方svvcs代码)
  • (linux驱动学习 - 12). IIC 驱动实验
  • .net framework 3.5sp1安装错误进度条不动怎么办
  • 【Python技术】利用akshare定时获取股票实时价,低于5日线钉钉通知报警