Android Studio点餐项目

 

要求:拖动预算条能够显示价格,也能根据口味和价格查询菜品,点击下一页时提示菜品和用户名

文件结构

 

代码如下

MainActivity.java

package com.example.task1114;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.task1114.model.Food;
import com.example.task1114.model.Person;

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

public class MainActivity extends AppCompatActivity {
     private Button task_searchbutton;
     private EditText task_edittext_name;
     private RadioGroup task_radiogroup;
     private RadioButton task_radiobutton_male,task_radiobutton_female;
     private CheckBox task_checkbox_sweet,task_checkbox_sour,task_checkbox_hot;
     private SeekBar task_seekbar;
     private ImageView task_imageview;
     private ToggleButton task_togglebutton;
     private List<Food> foodsList;
     private List<Food> foodsresult;
     private Person person;
     private boolean isHot;
     private boolean isSour;
     private boolean isSweet;
     private int price;
     private int CurrentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        initDate();
       setListenner();

    }

    private void initDate() {
        foodsList = new ArrayList<>();
        foodsList.add(new Food("桂林米线", 15, R.drawable.guilin, false, false, true));
        foodsList.add(new Food("红烧肉", 40, R.drawable.hongshaorou, false, true, false));
        foodsList.add(new Food("麻辣火锅", 95, R.drawable.malahuoguo, true, false, false));
        foodsList.add(new Food("西芹", 20, R.drawable.xiqin, true, false, false));
        foodsList.add(new Food("麻辣香锅", 70, R.drawable.malaxiangguo, true, false, false));
        foodsList.add(new Food("水煮鱼", 45, R.drawable.shuizhuyu, true, false, false));
        foodsList.add(new Food("清蒸鲈鱼", 50, R.drawable.qingzhengluyu, false, true, false));
        foodsList.add(new Food("木须肉", 30, R.drawable.muxurou, true, false, false));
        foodsList.add(new Food("酸辣汤", 35, R.drawable.suanlatang, true, false, true));
        foodsList.add(new Food("酸菜牛肉面", 10, R.drawable.suncainiuroumian, true, false, true));
        foodsList.add(new Food("娃娃菜", 25, R.drawable.wawacai, true, true, false));
        person = new Person();
    }

    private void findViews() {
        task_searchbutton = findViewById(R.id.task_searchbutton);
        task_edittext_name = findViewById(R.id.task_edittext_name);
        task_radiogroup = findViewById(R.id.task_radiogroup);
        task_radiobutton_male = findViewById(R.id.task_radiobutton_male);
        task_radiobutton_female = findViewById(R.id.task_radiobutton_female);
        task_checkbox_sweet = findViewById(R.id.task_checkbox_sweet);
        task_checkbox_sour = findViewById(R.id.task_checkbox_sour);
        task_checkbox_hot = findViewById(R.id.task_checkbox_hot);
        task_seekbar = findViewById(R.id.task_seekbar);
        task_imageview = findViewById(R.id.task_imageview);
        task_togglebutton = findViewById(R.id.task_togglebutton);
    }

    private void setListenner() {
        task_radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.task_radiobutton_male:
                        person.setSex("男");
                        break;
                    case R.id.task_radiobutton_female:
                        person.setSex("女");
                        break;
                }


            }
        });
        task_checkbox_hot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                isHot = b;
            }
        });
        task_checkbox_sour.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                isSour = b;
            }
        });
        task_checkbox_sweet.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                isSweet = b;
            }
        });
        task_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                price = task_seekbar.getProgress();
                Toast.makeText(MainActivity.this, "价格:" + price, Toast.LENGTH_SHORT).show();

            }
        });
        task_searchbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                search();

            }
        });
        task_togglebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String input = task_edittext_name.getText().toString();
                if(task_togglebutton.isChecked()){
                    //  下一个菜
                    CurrentIndex ++;
                    if(CurrentIndex < foodsresult.size()){
                        task_imageview.setImageResource(foodsresult.get(CurrentIndex).getPic());
                    } else {
                        Toast.makeText(MainActivity.this, "没有啦", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    // 显示信息:菜的名称
                    if(CurrentIndex < foodsresult.size()){
                        Toast.makeText(MainActivity.this, "菜名:" + foodsresult.get(CurrentIndex).getName()+";用户:"+input, Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, "没有啦", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
//        ?



        }

    private void search() {
        if (foodsresult == null) {
            foodsresult = new ArrayList<>();
        }

        // 先清除之前的结果
        foodsresult.clear();
        // 当前显示的是结果中的第几个菜
        CurrentIndex = 0;

        for (int index = 0; index < foodsList.size(); index++) {
            Food food = foodsList.get(index);
            if (food != null) {
                // 价格要小于设定的价格
                // 是顾客选择的口味
                if (food.getPrice() < price &&
                        (food.isHot() == isHot
                                || food.isSweet() == isSweet
                                || food.isSour() == isSour)
                ) {
                    foodsresult.add(food);
                }
            }
        }

        // 先显示第一张图片
        if (CurrentIndex < foodsresult.size()) {
            task_imageview.setImageResource(foodsresult.get(CurrentIndex).getPic());
        } else {
            task_imageview.setImageResource(R.drawable.ic_launcher_foreground);
        }
    }



}

Food.java

package com.example.task1114.model;

public class Food {
    private String name;
    private int price;
    private int pic;
    private boolean hot;
    private boolean sweet;
    private boolean sour;

    public Food(String name, int price, int pic, boolean hot, boolean sweet, boolean sour) {
        this.name = name;
        this.price = price;
        this.pic = pic;
        this.hot = hot;
        this.sweet = sweet;
        this.sour = sour;
    }

    public Food() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getPic() {
        return pic;
    }

    public void setPic(int pic) {
        this.pic = pic;
    }

    public boolean isHot() {
        return hot;
    }

    public void setHot(boolean hot) {
        this.hot = hot;
    }

    public boolean isSweet() {
        return sweet;
    }

    public void setSweet(boolean sweet) {
        this.sweet = sweet;
    }

    public boolean isSour() {
        return sour;
    }

    public void setSour(boolean sour) {
        this.sour = sour;
    }

    @Override
    public String toString() {
        return "Food{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", pic=" + pic +
                ", hot=" + hot +
                ", sweet=" + sweet +
                ", sour=" + sour +
                '}';
    }
}

Person.java

package com.example.task1114.model;

public class Person {
    private String name;
    private String sex;

    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Person(String name, String sex) {
        this.name = name;
        this.sex = sex;


    }

    public Person() {
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

布局文件activity_main.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"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
        android:background="#B0E0E6">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/task_name"
            android:textSize="27sp"
            android:textStyle="bold|italic"
            android:textColor="#ff00ff"
            android:gravity="center"
            android:layout_marginTop="10dp"
            >
        </TextView>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="horizontal"
            android:layout_weight="1"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/task_customer_name"
                android:textSize="20sp"></TextView>
            <EditText
                android:id="@+id/task_edittext_name"
                android:layout_width="180dp"
                android:layout_height="wrap_content"
                android:hint="@string/task_customer_hint"
                android:textSize="20sp"
                android:textColor="#cccccc">

            </EditText>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="horizontal"
            android:layout_weight="1">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/task_customer_sex"
                android:textSize="20sp"></TextView>
            <RadioGroup
                android:id="@+id/task_radiogroup"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:orientation="horizontal">
                <RadioButton
                    android:id="@+id/task_radiobutton_male"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/task_sex_male"
                    android:textSize="20sp"></RadioButton>
                <RadioButton
                    android:id="@+id/task_radiobutton_female"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/task_sex_female"
                    android:textSize="20sp"></RadioButton>
            </RadioGroup>


        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="horizontal"
            android:layout_weight="1"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/task_hobby"
                android:textSize="20sp"></TextView>
                <CheckBox
                    android:id="@+id/task_checkbox_sweet"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/task_hobby_sweet"
                    android:textSize="20sp"></CheckBox>
            <CheckBox
                android:id="@+id/task_checkbox_hot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/task_hobby_hot"
                android:textSize="20sp"></CheckBox>
            <CheckBox
                android:id="@+id/task_checkbox_sour"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/task_hobby_sour"
                android:textSize="20sp"></CheckBox>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/task_budget"
                android:textSize="20sp"
                ></TextView>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/task_mincost"
                android:textSize="20sp"></TextView>
            <SeekBar
                android:id="@+id/task_seekbar"
                android:layout_width="220dp"
                android:layout_height="wrap_content"
                android:max="100"></SeekBar>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/task_maxcost"
                android:textSize="20sp"></TextView>
        </LinearLayout>

            <Button
                android:id="@+id/task_searchbutton"
                android:layout_width="120dp"
                android:layout_height="50dp"
                android:text="@string/task_searchbutton"
               android:layout_gravity="center"
               ></Button>






    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/task_imageview"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher_foreground"></ImageView>
       <ToggleButton
           android:id="@+id/task_togglebutton"
           android:layout_width="120dp"
           android:layout_height="50dp"
           android:textOn="@string/task_listtogbutton"
           android:textOff="@string/task_nexttogbutton"
           android:textSize="20sp"
           android:layout_gravity="center"></ToggleButton>
    </LinearLayout>


</LinearLayout>

values下的stringsxml  //老师说为了提高可用性还是啥来着,走这定义中文😃

<resources>
    <string name="app_name">task1114</string>
    <string name="task_name">选餐系统</string>
    <string name="task_customer_name">姓名:</string>
    <string name="task_customer_hint">请输入用户名</string>
    <string name="task_customer_sex">性别:</string>
    <string name="task_sex_male">男</string>
    <string name="task_sex_female">女</string>
    <string name="task_hobby">喜好:</string>
    <string name="task_hobby_sour">酸</string>
    <string name="task_hobby_sweet">甜</string>
    <string name="task_hobby_hot">辣</string>
    <string name="task_budget">预算:</string>
    <string name="task_mincost">0元</string>
    <string name="task_maxcost">100元</string>
    <string name="task_searchbutton">查询菜品</string>
    <string name="task_listtogbutton">显示信息</string>
    <string name="task_nexttogbutton">下一页</string>

</resources>

演示

 

Android Studio是一款专门用于开发Android应用程序的集成开发环境(IDE)。要实现一个点餐系统,你可以按照以下步骤进行操作: 1. 创建一个新的Android项目: - 打开Android Studio并点击"Start a new Android Studio project"。 - 输入应用程序的名称和包名,并选择适当的项目位置。 - 选择目标设备和最低支持的Android版本。 - 选择一个空白活动模板并点击"Finish"。 2. 设计用户界面: - 在"res"文件夹中的"layout"文件夹下创建一个新的XML布局文件,用于欢迎界面。 - 使用Material Design的组件和样式来设计界面,例如使用CardView、Button、TextView等。 - 在布局文件中添加适当的控件和布局,以展示欢迎界面的内容。 3. 实现欢迎界面的逻辑: - 在Java代码中找到欢迎界面的活动类,并在其中编写逻辑代码。 - 在活动类中重写onCreate方法,并在其中设置布局文件。 - 在onCreate方法中添加逻辑代码,例如处理用户点击按钮的事件。 4. 实现点餐系统的功能: - 创建其他所需的活动类和布局文件,用于实现点餐系统的其他功能页面。 - 在活动类中编写逻辑代码,例如处理用户选择菜品、添加到购物车等操作。 - 使用适当的数据结构和算法来管理菜品列表、购物车和订单等数据。 5. 运行和测试应用程序: - 连接Android设备或启动模拟器。 - 点击Android Studio工具栏中的"Run"按钮,以运行应用程序。 - 在设备或模拟器上测试应用程序的各个功能,确保它们按预期工作。 这是一个简单的示例,你可以根据自己的需求和设计来扩展和完善点餐系统。希望这可以帮助你开始实现你的Android Studio点餐系统!
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值