效果展示:
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"><applicationandroid: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"><activityandroid:name=".MainActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name=".Register"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid: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;@Overrideprotected 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(){@Overridepublic 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();}elseToast.makeText(getApplicationContext(),"密码错误!", Toast.LENGTH_SHORT).show();}elseToast.makeText(getApplicationContext(),"用户名或密码不能为空!", Toast.LENGTH_SHORT).show();}elseToast.makeText(Login.this, "账号不存在,请注册!", Toast.LENGTH_SHORT).show();//关闭游标result.close();}});//注册register_but.setOnClickListener(new View.OnClickListener(){@Overridepublic 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;@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic 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);}@Overridepublic void onCreate(SQLiteDatabase db){//创建数据库用户表,并添加三个字段id,Account,PasswordString 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;}@Overridepublic 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>();@Overrideprotected 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(){@Overridepublic 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();elseToast.makeText(getApplicationContext(),"两次密码输入不一致",Toast.LENGTH_SHORT).show();} elseToast.makeText(getApplicationContext(),"密码不能为空!",Toast.LENGTH_SHORT).show();}} elseToast.makeText(getApplicationContext(),"账号不能为空!",Toast.LENGTH_SHORT).show();}});//返回back.setOnClickListener(new View.OnClickListener(){@Overridepublic 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"><TextViewandroid: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"/><EditTextandroid: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" /><EditTextandroid: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" /><Buttonandroid: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" /><Buttonandroid: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"><!-- 标题栏 --><RelativeLayoutandroid:id="@+id/title_layout"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid: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><!-- 菜单列表 --><!-- 购物车和结算按钮 --><ListViewandroid:id="@+id/menu_list"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /><RelativeLayoutandroid:id="@+id/cart_layout"android:layout_width="match_parent"android:layout_height="70dp"android:layout_marginTop="10dp"android:background="#ffffff"android:padding="0dp"><TextViewandroid: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" /><Buttonandroid: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><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="70dp"android:layout_marginTop="10dp"android:background="#ffffff"android:padding="0dp"><EditTextandroid: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"><TextViewandroid: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" /><EditTextandroid: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" /><EditTextandroid: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" /><EditTextandroid: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" /><Buttonandroid: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" /><Buttonandroid: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效果展示