点菜助手复盘总结(根据慕课网教程)

课程来源:Android零基础入门(其中的UI基础入门的综合型实验)
界面设计源码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
        android:text="客观请点菜" android:textColor="@color/purple" android:textSize="25sp"
        android:textStyle="bold" android:typeface="monospace"
        android:gravity="center"
        />
    <!--第二排-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="vertical"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            >
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="姓名"
                />
            <EditText
                android:id="@+id/et_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="请输入姓名"
                android:textSize="15sp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="性别"/>

            <RadioGroup
                android:id="@+id/rg_sex"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <RadioButton
                    android:id="@+id/rb_man"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""/>
                <RadioButton
                    android:id="@+id/rb_woman"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""/>
            </RadioGroup>
        </LinearLayout>
        <!--第四排-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="喜好"/>
            <CheckBox
                android:id="@+id/hot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""/>
            <CheckBox
                android:id="@+id/fish"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="海鲜"/>
            <CheckBox
                android:id="@+id/sour"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""/>
        </LinearLayout>
        <!--第五排-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="预算"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="0元"
                android:gravity="right"/>
            <SeekBar
                android:id="@+id/sb_price"
                android:layout_width="0dp"
                android:layout_weight="3"
                android:layout_height="wrap_content"
                android:max="100"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="100元"/>
            <TextView
                android:id="@+id/SN"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
            <!--第六排-->
        </LinearLayout>
        <Button
            android:id="@+id/btn_find"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="寻找菜品"
            android:background="@color/purple"
            android:layout_margin="5dp"/>
    </LinearLayout>
    <!--第三排-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="vertical"
    >
        <!--第七排-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <ImageView
                android:id="@+id/iv_pic"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:src="@mipmap/ic_launcher"/>
            <Button
                android:id="@+id/tb_click"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="下一个"
            />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

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

MainActivity.java:

package com.example.lenovo.ui_event;

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.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends initView {
    private RadioGroup rgSex;
    private CheckBox hot;
    private CheckBox fish;
    private CheckBox sour;
    private SeekBar sbPrice;
    private Button btnFind;
    private ImageView ivPic;
    private Button tbClick;
    private List<Food> lists_food;//本身有的菜品集合
    private List<Food> lists_get;//找出的菜品集合
    private Person person;//初始化person对象
    private RadioGroupListener radioGroupListener;
    private boolean isHot;//储存辣,酸,海鲜的布尔值
    private boolean isSour;
    private boolean isFish;
    private CheckBoxListener checkBoxListener;
    private int price = 30;
    private SeekBarListener seekBarListener;
    private ButtonListener buttonListener;
    private int count;//为要显示的第几个list_get的下标值
    private TextView sn;//pirce显示部分
    private static final String TAG = "MainActivity";//为后面的logd部分定义当前类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food);
        //初始化控件
        initView();
        //初始化数据
        initData();
        //为控件添加监听器
        setListener();
    }
    private void initView() {
        rgSex = (RadioGroup) findViewById(R.id.rg_sex);
        hot = (CheckBox) findViewById(R.id.hot);
        fish = (CheckBox) findViewById(R.id.fish);
        sour = (CheckBox) findViewById(R.id.sour);
        sbPrice = (SeekBar) findViewById(R.id.sb_price);
        sbPrice.setProgress(30);
        btnFind = (Button) findViewById(R.id.btn_find);
        ivPic = (ImageView) findViewById(R.id.iv_pic);
        tbClick = (Button) findViewById(R.id.tb_click);
        sn=(TextView)findViewById(R.id.SN);
    }

    private void initData() {
        person = new Person();//将person类实例化
        lists_food = new ArrayList<>();
        lists_get = new ArrayList<>();
        //下面就相当于本APP的数据库部分
        lists_food.add(new Food("麻辣香锅", 55, R.drawable.malaxiangguo, true, false, false));
        lists_food.add(new Food("水煮鱼", 48, R.drawable.shuizhuyu, true, true, false));
        lists_food.add(new Food("麻辣火锅", 80, R.drawable.malahuoguo, true, true, false));
        lists_food.add(new Food("清蒸鲈鱼", 68, R.drawable.qingzhengluyu, false, true, false));
        lists_food.add(new Food("桂林米粉", 15, R.drawable.guilin, false, false, false));
        lists_food.add(new Food("上汤娃娃菜", 28, R.drawable.wawacai, false, false, false));
        lists_food.add(new Food("红烧肉", 60, R.drawable.hongshaorou, false, false, false));
        lists_food.add(new Food("木须肉", 40, R.drawable.muxurou, false, false, false));
        lists_food.add(new Food("酸菜牛肉面", 35, R.drawable.suncainiuroumian, false, false, true));
        lists_food.add(new Food("西芹炒百合", 38, R.drawable.xiqin, false, false, false));
        lists_food.add(new Food("酸辣汤", 40, R.drawable.suanlatang, true, false, true));
    }

    private void setListener() {
        //为单选按钮添加事件
        radioGroupListener = new RadioGroupListener();
        rgSex.setOnCheckedChangeListener(radioGroupListener);

        checkBoxListener = new CheckBoxListener();
        fish.setOnCheckedChangeListener(checkBoxListener);
        hot.setOnCheckedChangeListener(checkBoxListener);
        sour.setOnCheckedChangeListener(checkBoxListener);

        seekBarListener = new SeekBarListener();
        sbPrice.setOnSeekBarChangeListener(seekBarListener);

        buttonListener = new ButtonListener();
        btnFind.setOnClickListener(buttonListener);
        tbClick.setOnClickListener(buttonListener);
    }

    //单选按钮监听事件
    class RadioGroupListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            //当用户选择当前RadioGroup组的Button时被触发
            switch (checkedId) {
                case R.id.rb_man:
                    person.setSex("男");//将数据传入person对象
                    break;
                case R.id.rb_woman:
                    person.setSex("女");
                    break;
            }
            System.out.println("性别:" + person.getSex());
        }
    }

    class CheckBoxListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //当控件状态改变时被触发
            CheckBox cbBox = (CheckBox) buttonView;
            switch (cbBox.getId()) {
                case R.id.fish:
                    if (isChecked) {
                        isFish = true;
                    } else {
                        isFish = false;
                    }
                    break;
                case R.id.sour:
                    if (isChecked) {
                        isSour = true;
                    } else {
                        isSour = false;
                    }
                    break;
                case R.id.hot:
                    if (isChecked) {
                        isHot = true;
                    } else {
                        isHot = false;
                    }
                    break;
            }
            System.out.println("当前喜好:" + "辣:" + isHot + " 海鲜:" + isFish + " 酸"
                    + isSour);
        }
    }

    class SeekBarListener implements SeekBar.OnSeekBarChangeListener {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            price = progress;//直接获取当前进度
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {//结束再显示数据
            //Toast.makeText(MainActivity.this, "价格:" + price, Toast.LENGTH_SHORT).show();
            sn.setText(String.valueOf(price));//price是int型的,要转为string类型的
            Log.d(TAG, "当前价格:" + price);
        }
    }

    class ButtonListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_find:
                    //当用户点击寻找时,需要筛选信息,并把结果显示在imageView上
                    checkData();//查找数据,并找到相应的图片
                    showPic();//展示图片
                    break;
                case R.id.tb_click:
                    if(lists_get.size()==0){
                        ivPic.setImageResource(R.mipmap.ic_launcher);
                    }
                    else if(count<lists_get.size()){
                        ivPic.setImageResource(lists_get.get(count).getPic());
                        count++;
                    }else{
                        count=0;//重头开始再次寻找
                        ivPic.setImageResource(lists_get.get(count).getPic());
                        count++;
                    }
                    Log.d(TAG, "count为:"+count);
                    break;
            }

        }

        private void checkData() {
            //找出菜品
            lists_get.clear();//每次点击的时候先清空lists_get集合
            count=1;//将count归1
            Log.d(TAG, "开始寻找******我是分割线*************");
            for (int i = 0; i < lists_food.size(); i++) {
                Food food = lists_food.get(i);
                if ((food.getPrice() <= price) && food.isFish() == isFish && food.isHot() == isHot && food.isSour() == isSour) {
                    lists_get.add(food);//如果找到符合用户条件的菜品就加入到get集合当中
                }
            }
            if (lists_get.size() == 0) {
                Toast.makeText(MainActivity.this, "对不起,没有找到相应的图片", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(MainActivity.this, "成功找到"+lists_get.size()+"份这样的菜品", Toast.LENGTH_SHORT).show();
            }
            //System.out.println("" + lists_get.size());
            Log.d(TAG, "当前list_get的长度为" + lists_get.size());
            //Log.d(TAG, "当前list_food的长度为" + lists_food.size());//铁定为11
        }

        private void showPic() {
            ivPic.setImageResource(R.mipmap.ic_launcher);//每次点击的时候初始化
            if (lists_get.size() == 0) {
                Log.d(TAG, "**************没有找到图片***************");
            } else {
                ivPic.setImageResource(lists_get.get(0).getPic());
            }
        }

    }
}

Person类:

package com.example.lenovo.ui_event;

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

    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 Food getFood() {
        return food;
    }

    public void setFood(Food food) {
        this.food = food;
    }

}

Food类:

package com.example.lenovo.ui_event;
public class Food {
    private String name;
    private boolean hot;
    private boolean fish;
    private boolean sour;
    private int price;
    private int pic;

    public String getName() {
        return name;
    }

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

    public boolean isHot() {
        return hot;
    }

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

    public boolean isFish() {
        return fish;
    }

    public void setFish(boolean fish) {
        this.fish = fish;
    }

    public boolean isSour() {
        return sour;
    }

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

    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;
    }

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

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

最终效果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值