0


android外卖点餐界面(期末作业)

效果展示:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

AndroidMainFest.xml

<?xml version="1.0" encoding="utf-8"?><manifestxmlns: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><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity><activityandroid:name=".Register"android:exported="true"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity><activityandroid:name=".Login"android:exported="true"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity></application></manifest>

FoodItem.java

packagecom.lzcu.endactivity;publicclassFoodItem{privateString name;// 食品名称privatedouble price;// 食品价格privateboolean isVegetarian;// 是否素食publicFoodItem(String name,double price){this.name = name;this.price = price;}publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}publicdoublegetPrice(){return price;}publicvoidsetPrice(double price){this.price = price;}}

Login.java

packagecom.lzcu.endactivity;importandroidx.appcompat.app.AppCompatActivity;importandroid.annotation.SuppressLint;importandroid.content.Context;importandroid.content.Intent;importandroid.content.SharedPreferences;importandroid.database.Cursor;importandroid.graphics.Color;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.TextView;importandroid.widget.Toast;publicclassLoginextendsAppCompatActivity{Button login_but,register_but;TextView touchScreen;EditTextAccount_2,pwd;MyDatabaseHelper myDatabaseHelper;@OverrideprotectedvoidonCreate(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 =newMyDatabaseHelper(this);
        myDatabaseHelper.getWritableDatabase();//只读//登录
        login_but.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(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 =newIntent(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(newView.OnClickListener(){@OverridepublicvoidonClick(View v){//显示注册界面Intent intent =newIntent(Login.this,Register.class);//启动显示修改界面startActivity(intent);finish();}});}}

MainActivity.java

packagecom.lzcu.endactivity;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.ArrayAdapter;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.ListView;importandroid.widget.TextView;importandroid.widget.Toast;importjava.util.ArrayList;importjava.util.List;publicclassMainActivityextendsAppCompatActivity{publicTextView txtCartItems;publicButton btnCheckout;publicArrayList<FoodItem> cartItems =newArrayList<>();publicdouble totalPrice =0;privateListView menu_list;privateList<String> mDataList;privateArrayAdapter<String> mAdapter;privateEditText mEditText;@OverrideprotectedvoidonCreate(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 =newArrayList<>();// 添加测试数据
        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 =newArrayAdapter<>(this,android.R.layout.simple_list_item_1, mDataList);// 设置适配器
        menu_list.setAdapter(mAdapter);// 设置列表项点击事件
        menu_list.setOnItemClickListener(newAdapterView.OnItemClickListener(){@OverridepublicvoidonItemClick(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 =newFoodItem("汉堡",22.5);FoodItem item2 =newFoodItem("薯条",12.0);addToCart(item1);addToCart(item2);}});// 更新购物车信息updateCart();// 结算按钮的点击事件
        btnCheckout.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(View v){checkout();}});}// 添加商品到购物车privatevoidaddToCart(FoodItem item){
        cartItems.add(item);
        totalPrice += item.getPrice();}// 更新购物车信息privatevoidupdateCart(){String cartText ="购物车("+ cartItems.size()+"): ¥"+ totalPrice;
        txtCartItems.setText(cartText);}// 结算privatevoidcheckout(){// 将购物车中的商品生成订单,进行结算等操作Toast.makeText(this,"订单已提交,感谢您的惠顾!",Toast.LENGTH_SHORT).show();
        cartItems.clear();
        totalPrice =0;updateCart();}}

MyDatabaseHelper.java

packagecom.lzcu.endactivity;importandroid.content.Context;importandroid.database.Cursor;importandroid.database.sqlite.SQLiteDatabase;importandroid.database.sqlite.SQLiteOpenHelper;importandroidx.annotation.Nullable;publicclassMyDatabaseHelperextendsSQLiteOpenHelper{//声明数据库的名字和表名privatestaticfinalStringDATABASENAME="Practical.db";// 数据库名称privatestaticfinalintDATABASEVERSION=1;privatestaticfinalStringTABLE_USERNAME="User";// 用户表名称publicMyDatabaseHelper(@NullableContext context){super(context,DATABASENAME,null,DATABASEVERSION);}@OverridepublicvoidonCreate(SQLiteDatabase db){//创建数据库用户表,并添加三个字段id,Account,PasswordString sqlString="create table "+TABLE_USERNAME+"( id integer primary key autoincrement,Account varchar(255),Password varchar(255))";
        db.execSQL(sqlString);}//注册publicvoidRegister(StringAccount,StringPassword){SQLiteDatabase db=super.getWritableDatabase();String sqlString="insert into "+TABLE_USERNAME+"(Account,Password) values (?,?)";//构造占位符的参数数组Object args[]=newObject[]{Account,Password};
        db.execSQL(sqlString,args);//此方法执行的sql语句主要有创建、插入、修改、删除等
        db.close();}//查询账号的方法publicCursorfind(){SQLiteDatabase db=super.getWritableDatabase();String sqlString="select * from User;";//执行查询,返回数据给游标Cursor result =db.rawQuery(sqlString,null);return result;}@OverridepublicvoidonUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){}}

Register.java

packagecom.lzcu.endactivity;importandroidx.appcompat.app.AppCompatActivity;importandroid.annotation.SuppressLint;importandroid.content.Intent;importandroid.database.Cursor;importandroid.graphics.Color;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.TextView;importandroid.widget.Toast;importjava.util.ArrayList;importjava.util.Timer;importjava.util.TimerTask;publicclassRegisterextendsAppCompatActivity{Button register_lj,back;TextView touchScreen;EditTextAccount,Pwd1,Pwd2;MyDatabaseHelper myDatabaseHelper;ArrayList<User> data =newArrayList<User>();@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.register);//数据库
        myDatabaseHelper =newMyDatabaseHelper(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(newView.OnClickListener(){@OverridepublicvoidonClick(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();elseif(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 =newIntent(Register.this,Login.class);startActivity(intent);finish();Toast.makeText(Register.this,"注册成功",Toast.LENGTH_SHORT).show();}elseif(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(newView.OnClickListener(){@OverridepublicvoidonClick(View v){//显示注册界面Intent intent =newIntent(Register.this,Login.class);//启动显示修改界面startActivity(intent);finish();}})}}

User.java

packagecom.lzcu.endactivity;publicclassUser{privateint id;privateString account;privateString password;publicUser(){}publicUser(int id,String account,String password){this.id = id;this.account = account;this.password = password;}publicintgetId(){return id;}publicvoidsetId(int id){this.id = id;}publicStringgetAccount(){return account;}publicvoidsetAccount(String account){this.account = account;}publicStringgetPassword(){return password;}publicvoidsetPassword(String password){this.password = password;}}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns: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

<LinearLayoutxmlns: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.ConstraintLayoutxmlns: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效果展示

标签: android

本文转载自: https://blog.csdn.net/yfq_29/article/details/132460406
版权归原作者 祈愿lucky 所有, 如有侵权,请联系我们删除。

“android外卖点餐界面(期末作业)”的评论:

还没有评论