网络资源模板--基于Android Studio 实现的咖啡点餐App

目录

一、测试环境说明

二、项目简介

三、项目演示

四、部设计详情(部分))

登录页面

注册页面

首页

我的页面

五、项目源码


一、测试环境说明

二、项目简介

这款基于Android平台的咖啡点餐应用采用了模块化设计理念,通过Fragment和BottomNavigationView构建了清晰的功能架构。

在技术实现上,项目整合了SharedPreferences数据存储、SQLite数据库管理以及SAX解析等核心技术,同时运用单例模式优化了数据操作流程。

系统实现了从用户登录、商品浏览到购物车管理和订单处理的全流程功能,特别注重交互体验的优化,如记住密码、实时价格计算等功能。

整体设计体现了良好的代码复用性和数据一致性,为用户提供了流畅的点餐体验,完整覆盖了移动端餐饮类应用的核心业务场景。

三、项目演示

网络资源模板--基于Android studio 咖啡点餐App

四、部设计详情(部分)

登录页面

java 复制代码
package com.example.login.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.login.R;
import com.example.login.utils.UserInfo;

public class LoginActivity extends AppCompatActivity {
    private EditText et_username;
    private EditText et_password;
    private CheckBox checkBox;
    private boolean is_login;
    private SharedPreferences mySharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mySharedPreferences = getSharedPreferences("user", MODE_PRIVATE);

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

        // 是否勾选记住密码
        is_login = mySharedPreferences.getBoolean("is_login", false);
        if (is_login) {
            String username=mySharedPreferences.getString("username", "");
            String password=mySharedPreferences.getString("password", "");
            et_username.setText(username);
            et_password.setText(password);
            checkBox.setChecked(true);
        }

        // 点击注册
        findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到注册页面
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        // 登录
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = et_username.getText().toString().trim();
                String password = et_password.getText().toString().trim();

                if (TextUtils.isEmpty(username) && TextUtils.isEmpty(password)) {
                    Toast.makeText(LoginActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                } else {
                    String name=mySharedPreferences.getString("username","");
                    String pwd=mySharedPreferences.getString("password","");


                    if (username.equals(name)&&password.equals(pwd)) {
                        SharedPreferences.Editor editor = mySharedPreferences.edit();
                        editor.putBoolean("is_login", is_login);
                        editor.putString("username", username);
                        editor.putString("password", password);
                        editor.commit();//提交

                        UserInfo user = new UserInfo(username, username, password, password);
                        UserInfo.setMyUserInfo(user);

                        // 登录成功
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        // CheckBox 点击事件
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_login = isChecked;
            }
        });

    }
}

实现了Android应用的标准登录功能,涵盖界面交互、数据验证、状态存储及页面跳转等核心逻辑。

通过SharedPreferences轻量级存储实现"记住密码"的持久化功能,提升用户体验。

  1. 用户界面与初始化

该代码是Android平台的登录界面实现,主要包含用户名输入框、密码输入框、记住密码复选框、登录按钮和注册跳转按钮。

界面初始化时,会检查本地存储中是否保存了用户上次登录的账号和密码,如果存在则自动填充到输入框中,并将"记住密码"复选框设为选中状态。

  1. 注册功能跳转

点击注册按钮后会跳转到注册页面(RegisterActivity),方便新用户进行账号注册。

这一功能通过Intent实现页面切换,未涉及具体注册逻辑,仅完成页面导航。

  1. 登录验证与数据处理

用户点击登录按钮时,会校验输入的用户名和密码是否为空。

若不为空,则与本地存储(SharedPreferences)中保存的账号密码进行比对。

验证成功后,会根据用户是否勾选"记住密码"来更新本地存储的登录状态,并将用户信息保存到全局的UserInfo类中。

登录成功则跳转到主页面(MainActivity),失败则提示错误信息。

  1. 记住密码功能

通过复选框的选中状态控制是否保存用户登录信息。

勾选时,登录成功后会持久化存储用户名和密码;未勾选时,仅临时使用输入信息登录,不更新本地存储。

这一功能依赖于SharedPreferences的读写操作,确保用户下次打开应用时可快速登录。

注册页面

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"
    tools:context=".activity.LoginActivity">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/fengmian" />

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

        <LinearLayout
            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="60dp"
                android:layout_height="40dp"
                android:layout_gravity="center_vertical"
                android:src="@mipmap/people" />

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

        </LinearLayout>
        <LinearLayout
            android:layout_marginTop="10dp"
            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="60dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:src="@mipmap/mima" />

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



        <Button
            android:id="@+id/register"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="20dp"
            android:text="注册"/>

    </LinearLayout>
</LinearLayout>

完成了注册功能的基础流程,涵盖输入、存储和页面跳转,适合初学者理解Android数据持久化。

  1. 界面与数据初始化

该代码实现了Android应用的注册功能界面,包含用户名输入框(`et_username`)和密码输入框(`et_password`)。

初始化时通过`SharedPreferences`加载本地存储(键为`user`),用于后续保存用户注册信息。

布局文件(`activity_register`)定义了注册页面的UI结构。

  1. 注册逻辑与输入校验

用户点击注册按钮后,系统会提取输入的用户名和密码,并进行非空校验。

若任一字段为空,则弹出Toast提示"请输入用户名或密码";若校验通过,则将数据通过`SP`存入本地,键分别为`username`和`password`,提交后提示"注册成功,请登录"。

  1. 数据存储与页面管理

注册成功时,数据会以明文形式持久化到`SP`中(无加密措施)。

存储完成后,通过`finish()`关闭当前页面,返回登录界面,引导用户使用刚注册的账号登录。

  1. 安全与扩展性缺陷

代码未对密码进行加密存储,存在安全风险;未校验用户名是否已存在,可能导致重复注册;

缺乏密码复杂度规则(如长度、特殊字符等)。

后续可增加数据库存储、网络请求或加密库(如Android Keystore)来提升安全性。

首页

java 复制代码
package com.example.login.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.login.R;
import com.example.login.activity.CoffeeDetailsActivity;
import com.example.login.adapter.LeftListAdapter;
import com.example.login.adapter.RightListAdapter;
import com.example.login.utils.Data;
import com.example.login.utils.CoffeeInfo;

import java.util.ArrayList;
import java.util.List;


public class HomeFragment extends Fragment {
//声明变量
    private View view;
    private ListView leftListView, rightListView;
    private LeftListAdapter myLeftListAdapter;
    private RightListAdapter myRightListAdapter;
    private List<String> leftDataList = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, null);
        //初始化控件
        leftListView = view.findViewById(R.id.leftListView);
        leftListView.setAdapter(myLeftListAdapter);
        rightListView = view.findViewById(R.id.rightListView);
        rightListView.setAdapter(myRightListAdapter);
        rightListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CoffeeInfo data = myRightListAdapter.getItem(position);
                Intent intent = new Intent(getActivity(), CoffeeDetailsActivity.class);
                intent.putExtra("DATA",data);
                startActivity(intent);
            }
        });
        changeRightDatas(0);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        leftDataList.clear();
        leftDataList.add("人气Top");
        leftDataList.add("招牌必点");
        leftDataList.add("经典推荐");
        leftDataList.add("甜品小点");
        myLeftListAdapter = new LeftListAdapter(leftDataList);
        myLeftListAdapter.setMyLeftListOnClickItemListener(new LeftListAdapter.LeftListOnClickItemListener() {
            @Override
            public void onItemClick(int position) {
                changeRightDatas(position);
            }
        });
        myRightListAdapter = new RightListAdapter();
    }

    private void changeRightDatas(int position) {
        myRightListAdapter.setmListData(Data.getListData(position));
        myRightListAdapter.notifyDataSetChanged();
    }
}

实现了一个典型的电商类目浏览功能,核心特点是双列表联动交互。

适合学习Fragment、Adapter、数据绑定及组件通信等Android开发基础。

  1. 界面布局与初始化

实现了一个双列表联动的咖啡菜单界面,采用左右分栏设计。

左侧列表展示咖啡分类(如"人气Top"、"招牌必点"等),右侧列表显示对应分类下的具体咖啡商品。

通过onCreateView初始化视图,并在onCreate中预加载左侧分类数据。

  1. 数据加载与适配器配置
  • 左侧列表:通过LeftListAdapter绑定静态分类数据(leftDataList),并设置点击监听器,点击时触发右侧列表数据更新。

  • 右侧列表:使用RightListAdapter动态加载数据,通过Data.getListData(position)根据左侧选中分类获取对应咖啡商品列表(如拿铁、美式等),并通过notifyDataSetChanged刷新显示。

  1. 交互逻辑与跳转功能
  • 分类切换:点击左侧分类时,调用实时更新右侧列表内容。

  • 商品详情:点击右侧列表项时,通过Intent跳转到CoffeeDetailsActivity,并携带当前咖啡商品数据,实现详情页的数据传递。

  1. 代码结构与扩展性
  • 采用MVP模式分离数据与视图逻辑,便于后续扩展。

  • 通过接口回调实现左右列表联动,耦合度低。

  • 未体现网络请求或数据库操作,当前数据为本地模拟,实际应用中需替换为动态数据源。

我的页面

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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#633D04"
        android:gravity="center_vertical">


        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@mipmap/face"/>

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

            <TextView
                android:id="@+id/tv_username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="啡梵星咖"
                android:textSize="30dp"
                android:textColor="@color/white"/>

            <TextView
                android:id="@+id/tv_nickname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="这个人很懒,什么也没有留下~"
                android:textColor="@color/white"/>

        </LinearLayout>

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="#E8E7E7"/>


    <RelativeLayout
        android:id="@+id/tv_contact"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="48dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="联系我们"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_baseline_keyboard_arrow_right_24"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_alignParentBottom="true"
            android:background="#DCDCDC" />



    </RelativeLayout>

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


    <RelativeLayout
        android:id="@+id/tv_introduce"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="48dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="店铺介绍" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_baseline_keyboard_arrow_right_24"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>



    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/tv_update"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="48dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="更新版本" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_baseline_keyboard_arrow_right_24"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>


    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/tv_about"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="48dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="关于APP"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_baseline_keyboard_arrow_right_24"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>


    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/tv_exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="50dp"
            android:background="#E8E7E7"
            android:gravity="center"
            android:text="退出登录"
            android:textColor="#47351A"
            android:textStyle="bold" />
    </RelativeLayout>



</LinearLayout>

提供了个人中心的基础框架,涵盖用户信息展示和常用功能跳转,适合快速搭建基础版"我的"页面。

重点展示了Fragment的生命周期管理、Intent跳转和多按钮事件处理。

  1. 用户信息展示与界面初始化

该代码实现了一个个人中心页面,顶部显示用户欢迎信息,下方提供多个功能入口。

通过onCreateView加载布局,并从全局UserInfo类获取当前登录用户信息,动态更新界面显示。

采用RelativeLayout布局功能按钮,包括联系客服、应用介绍、检查更新、关于我们和退出登录。

  1. 多功能交互实现
  • 联系客服:点击后调用系统拨号界面,预设号码为123456789。

  • 应用介绍:跳转到IntroductionActivity展示应用功能说明。

  • 检查更新:模拟版本检测,仅弹出Toast提示"当前为最新版本"。

  • 关于我们:跳转至AboutActivity显示应用版本、开发者等信息。

  • 退出登录:返回登录页并关闭当前页面,清空用户会话状态。

  1. 代码设计特点
  • 统一实现OnClickListener接口管理所有按钮点击事件,通过switch-case区分操作。

  • 用户数据通过单例类UserInfo传递,保证信息一致性。

  • 隐式Intent调用系统拨号功能,体现Android原生能力集成。

五、项目源码

👇👇👇👇👇快捷方式👇👇👇👇👇

相关推荐
androidwork2 小时前
深入解析内存抖动:定位与修复实战(Kotlin版)
android·kotlin
梦天20152 小时前
android核心技术摘要
android
szhangbiao4 小时前
“开发板”类APP如果做屏幕适配
android
高林雨露5 小时前
RecyclerView中跳转到最后一条item并确保它在可视区域内显示
android
移动开发者1号7 小时前
ReLinker优化So库加载指南
android·kotlin
山野万里__7 小时前
C++与Java内存共享技术:跨平台与跨语言实现指南
android·java·c++·笔记
Huckings7 小时前
Android 性能问题
android
移动开发者1号8 小时前
剖析 Systrace:定位 UI 线程阻塞的终极指南
android·kotlin
移动开发者1号8 小时前
深入解析内存抖动:定位与修复实战(Kotlin版)
android·kotlin
whysqwhw8 小时前
OkHttp深度架构缺陷分析与革命性演进方案
android