安卓综合案例:选餐系统

一:创建视图
代码如下:


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginStart="15dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:layout_marginLeft="15dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name"
            android:textSize="22sp"/>

        <EditText
            android:id="@+id/nameEditText"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:hint="@string/edit_text_input_hint_name"
            />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginLeft="15dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:layout_marginStart="15dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sex"
            android:textSize="22sp"/>

        <RadioGroup
            android:id="@+id/sexRadioGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

            <RadioButton
                android:id="@+id/maleRadioButton"
                android:textSize="22sp"
                android:layout_width="75dp"
                android:layout_height="wrap_content"
                android:text="@string/male"/>

            <RadioButton
                android:id="@+id/femaleRadioButton"
                android:textSize="22sp"
                android:layout_width="75dp"
                android:layout_height="wrap_content"
                android:text="@string/female"/>
        </RadioGroup>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginLeft="15dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:layout_marginStart="15dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="喜好"
            android:textSize="22sp"/>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <CheckBox
                android:id="@+id/hotCheckBox"
                android:text="辣"
                android:textSize="22sp"
                android:layout_width="65dp"
                android:layout_height="wrap_content"/>
            <CheckBox
                android:id="@+id/fishCheckBox"
                android:text="海鲜"
                android:textSize="22sp"
                android:layout_width="100dp"
                android:layout_height="wrap_content"/>
            <CheckBox
                android:id="@+id/sourCheckBox"
                android:text="酸"
                android:textSize="22sp"
                android:layout_width="65dp"
                android:layout_height="wrap_content"/>

        </LinearLayout>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginLeft="15dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="预算"
            android:textSize="22sp"/>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0元"
                android:textSize="22sp"/>
            <SeekBar
                android:id="@+id/seekBar"
                android:textSize="22sp"
                android:layout_width="220dp"
                android:max="100"
                android:layout_height="wrap_content"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="100元"
                android:textSize="22sp"/>

        </LinearLayout>

    </LinearLayout>

    <Button
        android:id="@+id/searchButton"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:text="寻找菜品"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:textSize="22sp"/>


</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="0dp"
    android:layout_weight="1">

    <ImageView
        android:id="@+id/foodImageView"
        android:src="@drawable/ic_launcher_foreground"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"/>
    <ToggleButton
        android:id="@+id/showToggleButton"
        android:textOff="下一个"
        android:textOn="显示信息"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:textSize="22sp"
        android:layout_width="300dp"
        android:layout_height="50dp"/>
</LinearLayout>
</LinearLayout>

试图效果为
在这里插入图片描述
代码如下:
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
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.RadioGroup;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.ToggleButton;

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

import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private EditText mNameEditText;
private RadioGroup mSexRadioGroup;
private CheckBox mHotCheckBox, mFishCheckBox, mSourCheckBox;
private SeekBar mSeekBar;
private Button mSearchButton;
private ImageView mFoodImageView;
private ToggleButton mToggleButton;
private List mFoods;
private Person mPerson;
private List mFoodResult;
private boolean mIsFish;
private boolean mIsSour;
private boolean mIsHot;
private int mPrice;
private int mCurrentIndex;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   // 初始化控件
    findViews();
    // 初始化数据
    initData();
    // 为控件添加监听器,实现基本功能
    setListeners();
}

private void initData() {
    // new 出来一个空的食物 list
    mFoods = new ArrayList<>();
    // 初始化添加所有的数据
    mFoods.add(new Food("麻辣香锅", 55, R.drawable.malaxiangguo, true, false, false));
    mFoods.add(new Food("水煮鱼", 48, R.drawable.shuizhuyu, true, true, false));
    mFoods.add(new Food("麻辣火锅", 80, R.drawable.malahuoguo, true, true, false));
    mFoods.add(new Food("清蒸鲈鱼", 68, R.drawable.qingzhengluyu, false, true, false));
    mFoods.add(new Food("桂林米粉", 15, R.drawable.guilin, false, false, false));
    mFoods.add(new Food("上汤娃娃菜", 28, R.drawable.wawacai, false, false, false));
    mFoods.add(new Food("红烧肉", 60, R.drawable.hongshaorou, false, false, false));
    mFoods.add(new Food("木须肉", 40, R.drawable.muxurou, false, false, false));
    mFoods.add(new Food("酸菜牛肉面", 35, R.drawable.suncainiuroumian, false, false, true));
    mFoods.add(new Food("西芹炒百合", 38, R.drawable.xiqin, false, false, false));
    mFoods.add(new Food("酸辣汤", 40, R.drawable.suanlatang, true, false, true));

    mPerson = new Person();

    mFoodResult = new ArrayList<>();
}

private void findViews() {
    mNameEditText = findViewById(R.id.nameEditText);
    mSexRadioGroup = findViewById(R.id.sexRadioGroup);
    mHotCheckBox = findViewById(R.id.hotCheckBox);
    mFishCheckBox = findViewById(R.id.fishCheckBox);
    mSourCheckBox = findViewById(R.id.sourCheckBox);
    mSeekBar = findViewById(R.id.seekBar);
    mSeekBar.setProgress(30);
    mSearchButton = findViewById(R.id.searchButton);
    mToggleButton = findViewById(R.id.showToggleButton);
    mToggleButton.setChecked(true);
    mFoodImageView = findViewById(R.id.foodImageView);
}
private void setListeners() {
    // 设置单选框listener
    mSexRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.maleRadioButton:
                    mPerson.setSex("男");
                    break;
                case R.id.femaleRadioButton:
                    mPerson.setSex("女");
                    break;
            }
        }
    });
    // 设置复选框listener
    mFishCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mIsFish = isChecked;
        }
    });
    mSourCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mIsSour = isChecked;
        }
    });
    mHotCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mIsHot = isChecked;
        }
    });
    mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        }


        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
            mPrice = seekBar.getProgress();
            Toast.makeText(MainActivity.this, "价格: " + mPrice, Toast.LENGTH_SHORT).show();
        }
    });
    mSearchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            search();
        }
    });
    mToggleButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if(mToggleButton.isChecked()){
                //  下一个菜
                mCurrentIndex ++;
                if(mCurrentIndex < mFoodResult.size()){
                    mFoodImageView.setImageResource(mFoodResult.get(mCurrentIndex).getPic());
                } else {
                    Toast.makeText(MainActivity.this, "没有啦", Toast.LENGTH_SHORT).show();
                }
            } else {
                // 显示信息:菜的名称
                if(mCurrentIndex < mFoodResult.size()){
                    Toast.makeText(MainActivity.this, "菜名: " + mFoodResult.get(mCurrentIndex).getName(), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "没有啦", Toast.LENGTH_SHORT).show();
                }
            }
        }
    });
}

private void search() {
    // 结果列表每次都清空
    // 遍历所有菜
    // 如果符合条件,则加入到我们的结果列表中
    // 如果为空,先初始化
    if(mFoodResult == null){
        mFoodResult = new ArrayList<>();
    }
    // 先清除之前的结果
    mFoodResult.clear();
    // 当前显示的是结果中的第几个菜
    mCurrentIndex = 0;
    for (int index = 0; index < mFoods.size(); index++) {
        Food food = mFoods.get(index);
        if(food != null){
            // 价格要小于设定的价格
            // 是顾客选择的口味
            if(food.getPrice() < mPrice && (food.isHot() ==  mIsHot || food.isFish() == mIsFish || food.isSour() == mIsSour)){
                mFoodResult.add(food);
            }
        }
    }
    // 先显示第一张图片
    if(mCurrentIndex < mFoodResult.size()){
        mFoodImageView.setImageResource(mFoodResult.get(mCurrentIndex).getPic());
    } else {
        mFoodImageView.setImageResource(R.drawable.ic_launcher_foreground);
    }
}}

还需在创建出一个food文件和一个person文件,如下图所示:
1.food文件为:
public class Food {
private String name;
private int price;
private int pic;
private boolean hot;
private boolean fish;
private boolean sour;

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

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 isFish() {
    return fish;
}

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

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 +
            ", fish=" + fish +
            ", sour=" + sour +
            '}';
}}

person文件为
public class Person {
private String name;
private String sex;
private 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;
}}

如此边完成点餐实例

猜你喜欢

转载自blog.csdn.net/Derrick_itRose/article/details/107465588