效果展示:
AndroidMainFest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.EndActivity"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Register"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
FoodItem.java
package com.lzcu.endactivity;
public class FoodItem {
private String name; // 食品名称
private double price; // 食品价格
private boolean isVegetarian; // 是否素食
public FoodItem(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
Login.java
package com.lzcu.endactivity;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Color;
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 Login extends AppCompatActivity {
Button login_but,register_but;
TextView touchScreen;
EditText Account_2,pwd;
MyDatabaseHelper myDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//按钮组件
login_but = findViewById(R.id.login);
register_but = findViewById(R.id.register);
//编辑框
Account_2 = findViewById(R.id.UserName);
pwd =findViewById(R.id.Pwd);
//数据库
myDatabaseHelper = new MyDatabaseHelper(this);
myDatabaseHelper.getWritableDatabase();//只读
//登录
login_but.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String acc = Account_2.getText().toString();
String pwd1 =pwd.getText().toString();
//获取数据库数据,判断用户名是否在库
Cursor result = myDatabaseHelper.find();
int a=0;
int b=0;
for (result.moveToFirst();!result.isAfterLast();result.moveToNext())
{
@SuppressLint("Range") String account_1=result.getString(result.getColumnIndex("Account"));
@SuppressLint("Range") String pwd_1=result.getString(result.getColumnIndex("Password"));
if (Account_2.getText().toString().equals(account_1) && pwd.getText().toString().equals(pwd_1))
a=1;
if (Account_2.getText().toString().equals(account_1))
b=1;
}
if (b==1)
{
if (!Account_2.getText().toString().equals("") && !pwd.getText().toString().equals(""))
{
if(a==1)
{
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
Toast.makeText(Login.this, "登入成功", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(getApplicationContext(),"密码错误!", Toast.LENGTH_SHORT).show();
}else
Toast.makeText(getApplicationContext(),"用户名或密码不能为空!", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(Login.this, "账号不存在,请注册!", Toast.LENGTH_SHORT).show();
//关闭游标
result.close();
}
});
//注册
register_but.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//显示注册界面
Intent intent = new Intent(Login.this, Register.class);
//启动显示修改界面
startActivity(intent);
finish();
}
});
}
}
MainActivity.java
package com.lzcu.endactivity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public TextView txtCartItems;
public Button btnCheckout;
public ArrayList<FoodItem> cartItems = new ArrayList<>();
public double totalPrice = 0;
private ListView menu_list;
private List<String> mDataList;
private ArrayAdapter<String> mAdapter;
private EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtCartItems = findViewById(R.id.txt_cart_items);
btnCheckout = findViewById(R.id.btn_checkout);
//初始化
// 初始化 ListView 和 数据源
menu_list = findViewById(R.id.menu_list);
mDataList = new ArrayList<>();
// 添加测试数据
mDataList.add("汉堡 22.5");
mDataList.add("薯条 12.0");
mDataList.add("热干面 8.0");
mDataList.add("麻辣香锅 59.0");
mDataList.add("鱼香肉丝 38.0");
mDataList.add("大盘鸡 48.0");
mDataList.add("炒粉丝 11.0");
mDataList.add("毛血旺 58.0");
// 创建适配器
mAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, mDataList);
// 设置适配器
menu_list.setAdapter(mAdapter);
// 设置列表项点击事件
menu_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, "你选择了" + mDataList.get(i),
Toast.LENGTH_SHORT).show();
// 获取 editText 控件
mEditText = findViewById(R.id.editText);
mEditText.append(mDataList.get(i));
// mEditText.setText("你选择了"+mDataList.get(i)+"吗?");
// 加入购物车的逻辑
FoodItem item1 = new FoodItem("汉堡", 22.5);
FoodItem item2 = new FoodItem("薯条", 12.0);
addToCart(item1);
addToCart(item2);
}
});
// 更新购物车信息
updateCart();
// 结算按钮的点击事件
btnCheckout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkout();
}
});
}
// 添加商品到购物车
private void addToCart(FoodItem item) {
cartItems.add(item);
totalPrice += item.getPrice();
}
// 更新购物车信息
private void updateCart() {
String cartText = "购物车(" + cartItems.size() + "): ¥" + totalPrice;
txtCartItems.setText(cartText);
}
// 结算
private void checkout() {
// 将购物车中的商品生成订单,进行结算等操作
Toast.makeText(this, "订单已提交,感谢您的惠顾!", Toast.LENGTH_SHORT).show();
cartItems.clear();
totalPrice = 0;
updateCart();
}
}
MyDatabaseHelper.java
package com.lzcu.endactivity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MyDatabaseHelper extends SQLiteOpenHelper
{
//声明数据库的名字和表名
private static final String DATABASENAME = "Practical.db" ; // 数据库名称
private static final int DATABASEVERSION = 1 ;
private static final String TABLE_USERNAME = "User" ; // 用户表名称
public MyDatabaseHelper(@Nullable Context context)
{
super(context, DATABASENAME, null, DATABASEVERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
//创建数据库用户表,并添加三个字段id,Account,Password
String sqlString="create table "+ TABLE_USERNAME+"( id integer primary key autoincrement,Account varchar(255),Password varchar(255))";
db.execSQL(sqlString);
}
//注册
public void Register(String Account,String Password)
{
SQLiteDatabase db=super.getWritableDatabase();
String sqlString="insert into "+TABLE_USERNAME+"(Account,Password) values (?,?)";
//构造占位符的参数数组
Object args[]=new Object[]{Account,Password};
db.execSQL(sqlString,args);//此方法执行的sql语句主要有创建、插入、修改、删除等
db.close();
}
//查询账号的方法
public Cursor find()
{
SQLiteDatabase db=super.getWritableDatabase();
String sqlString="select * from User;";
//执行查询,返回数据给游标
Cursor result =db.rawQuery(sqlString,null);
return result;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Register.java
package com.lzcu.endactivity;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
public class Register extends AppCompatActivity {
Button register_lj,back;
TextView touchScreen;
EditText Account,Pwd1,Pwd2;
MyDatabaseHelper myDatabaseHelper;
ArrayList<User> data = new ArrayList<User>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
//数据库
myDatabaseHelper = new MyDatabaseHelper(this);
//按钮组件
register_lj =findViewById(R.id.register_but);
back =findViewById(R.id.back);
//编辑框
Account = findViewById(R.id.register_name);
Pwd1 = findViewById(R.id.register_pwd);
Pwd2 = findViewById(R.id.register_pwd2);
//立即注册
register_lj.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//获取输入的用户名和密码
String name = Account.getText().toString().trim();
String password = Pwd1.getText().toString().trim();
//获取数据库数据,判断用户名是否已存在
Cursor result = myDatabaseHelper.find();
int a=0;
if (!Account.getText().toString().equals(""))
{
if (Account.getText().toString().length()<6)
Toast.makeText(Register.this, "账号创建不能少于6位", Toast.LENGTH_SHORT).show();
else if (Pwd1.getText().toString().length()<6)
Toast.makeText(Register.this, "密码设置不能少于6位", Toast.LENGTH_SHORT).show();
else
{
for (result.moveToFirst();!result.isAfterLast();result.moveToNext())
{
//判断数据库是否存在此对象
@SuppressLint("Range") String account_1 = result.getString(result.getColumnIndex("Account"));
if(Account.getText().toString().equals(account_1))
{
a=1;
}
}
//关闭游标
result.close();
if(!Pwd1.getText().toString().equals("") && !Pwd2.getText().toString().equals(""))
{
if ((Pwd1.getText().toString().equals(Pwd2.getText().toString()) && a==0))
{
myDatabaseHelper.Register(name,password);
Intent intent = new Intent(Register.this, Login.class);
startActivity(intent);
finish();
Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();
}
else if (a==1)
Toast.makeText(getApplicationContext(),"账号已存在,请重新输入账号",Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(),"两次密码输入不一致",Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(),"密码不能为空!",Toast.LENGTH_SHORT).show();
}
} else
Toast.makeText(getApplicationContext(),"账号不能为空!",Toast.LENGTH_SHORT).show();
}
});
//返回
back.setOnClickListener(
new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//显示注册界面
Intent intent = new Intent(Register.this, Login.class);
//启动显示修改界面
startActivity(intent);
finish();
}
})
}
}
User.java
package com.lzcu.endactivity;
public class User
{
private int id;
private String account;
private String password;
public User( ) {
}
public User(int id, String account, String password) {
this.id = id;
this.account = account;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity"
android:background="@drawable/img">
<TextView
android:id="@+id/text_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="外卖点餐"
android:textColor="@color/black"
android:textSize="30dp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<EditText
android:id="@+id/UserName"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:hint="请输入用户名"
android:padding="15dp"
android:singleLine="true"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_top" />
<EditText
android:id="@+id/Pwd"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="请输入用户密码"
android:inputType="textPassword"
android:maxLength="16"
android:padding="15dp"
android:singleLine="true"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/UserName" />
<Button
android:id="@+id/login"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_marginTop="30dp"
android:text="登录"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/register"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Pwd" />
<Button
android:id="@+id/register"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_marginTop="30dp"
android:text="注册"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/login"
app:layout_constraintTop_toBottomOf="@+id/Pwd" />
</androidx.constraintlayout.widget.ConstraintLayout>
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img">
<!-- 标题栏 -->
<RelativeLayout
android:id="@+id/title_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="外卖点餐系统"
android:textSize="22sp"
android:textColor="#ffffff" />
</RelativeLayout>
<!-- 菜单列表 -->
<!-- 购物车和结算按钮 -->
<ListView
android:id="@+id/menu_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<RelativeLayout
android:id="@+id/cart_layout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:padding="0dp">
<TextView
android:id="@+id/txt_cart_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="购物车(0): ¥0"
android:textColor="#000000"
android:textSize="18sp" />
<Button
android:id="@+id/btn_checkout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="结算"
android:textColor="#ffffff"
android:textSize="18sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:padding="0dp">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="25dp"
android:gravity="bottom"
/>
</RelativeLayout>
</LinearLayout>
register.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".Login"
android:background="@drawable/img"
tools:ignore="MissingDefaultResource">
<TextView
android:id="@+id/text_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:text="注册界面"
android:textColor="#141414"
android:textSize="30dp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/register_name"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:hint="请输入用户名"
android:padding="15dp"
android:singleLine="true"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_top" />
<EditText
android:id="@+id/register_pwd"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="请输入用户密码"
android:inputType="textPassword"
android:maxLength="16"
android:padding="15dp"
android:singleLine="true"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/register_name" />
<EditText
android:id="@+id/register_pwd2"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="请确认密码"
android:inputType="textPassword"
android:maxLength="16"
android:padding="15dp"
android:singleLine="true"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/register_pwd" />
<Button
android:id="@+id/register_but"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_marginTop="30dp"
android:text="立即注册"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/back"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/register_pwd2" />
<Button
android:id="@+id/back"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_marginTop="30dp"
android:text="返回登录"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/register_but"
app:layout_constraintTop_toBottomOf="@+id/register_pwd2" />
</androidx.constraintlayout.widget.ConstraintLayout>
xiao效果展示