界面 设置采用了线性布局
第一个是TextView
第二个是线性布局
使用的控件有1.EditText 2.SeekBar 3.CheckBox 4.ToggleButton 5.RadioGroup 6.Button 7.TextView 8.ImageView
<?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" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:background="#B9B9FF" android:gravity="center_horizontal" android:text="开始选餐" android:textSize="32sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_marginTop="15dp" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="vertical" android:layout_weight="1"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="15dp"> <TextView android:layout_width="50dp" android:layout_height="match_parent" android:textSize="22sp" android:text="姓名" android:gravity="center_vertical"/> <EditText android:id="@+id/mEditName" android:layout_width="230dp" android:layout_height="match_parent" android:hint="请输入姓名" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:orientation="horizontal"> <TextView android:layout_width="50dp" android:layout_height="match_parent" android:textSize="22sp" android:text="性别" android:gravity="center_vertical"/> <RadioGroup android:id="@+id/mRadioSex" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <RadioButton android:id="@+id/male" android:checked="true" android:layout_width="70dp" android:layout_height="match_parent" android:text="男"/> <RadioButton android:id="@+id/female" android:layout_width="70dp" android:layout_height="match_parent" android:text="女"/> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:orientation="horizontal"> <TextView android:layout_width="50dp" android:layout_height="match_parent" android:textSize="22sp" android:text="喜好" android:gravity="center_vertical"/> <CheckBox android:id="@+id/isHotCheck" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="辣"/> <CheckBox android:id="@+id/isFishCheck" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="海鲜"/> <CheckBox android:id="@+id/isSoulCheck" 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:layout_marginLeft="15dp" android:orientation="horizontal"> <TextView android:layout_width="50dp" android:layout_height="match_parent" android:textSize="22sp" android:text="预算" android:gravity="center_vertical"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0元"/> <SeekBar android:id="@+id/PriceSeekBar" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="100元" /> </LinearLayout> </LinearLayout> <Button android:id="@+id/selectFood" android:layout_width="220dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="15dp" android:text="寻找菜品" app:backgroundTint="#E1D6D6" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical"> <ImageView android:id="@+id/imageFood" android:layout_height="0dp" android:layout_width="match_parent" android:layout_weight="3"/> <ToggleButton android:id="@+id/showToggleButton" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="15dp" android:checked="true" android:textOn="显示信息" android:textOff="下一个" app:backgroundTint="#E1D6D6" /> </LinearLayout> </LinearLayout> </LinearLayout>
Java代码
第一步:初始化数据(init)用于获得控件
第二步:添加数据(initData)添加菜谱的数据
第三步: 设置按钮点击事件(setListener)
MainActivity.java
package com.example.ofoodselection; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; 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 java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private EditText mEditName; private RadioGroup mRadioSex; private CheckBox mIsHotCheck; private CheckBox mIsFishCheck; private CheckBox mIsSoulCheck; private SeekBar mPriceSeekBar; private Button mSelectFood; private ImageView mImageFood; private ToggleButton mShowToggleButton; private List<Food> mFoods; private List<Food> mFoodsResult; private Person mPerson; private Boolean mFish = false; private Boolean mHot = false; private Boolean mSoul = false; private int mPrice; private int mCurrentFoodIndex; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化按钮 init(); //初始化数据 initData(); //设置监听事件 setListener(); } private void setListener() { mEditName.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { mPerson.setName(s.toString()); } }); mRadioSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.male: mPerson.setSex("男"); case R.id.female: mPerson.setSex("女"); } } }); mIsHotCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mHot = isChecked; } }); mIsFishCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mFish = isChecked; } }); mIsSoulCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mSoul = isChecked; } }); mPriceSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { mPrice = seekBar.getProgress(); Toast.makeText(MainActivity.this, "价格:" + mPrice, Toast.LENGTH_SHORT).show(); } }); mSelectFood.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { findFood(); } }); mShowToggleButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mShowToggleButton.isChecked()){ mCurrentFoodIndex++; if (mCurrentFoodIndex < mFoodsResult.size()) { mImageFood.setImageResource(mFoodsResult.get(mCurrentFoodIndex).getPic()); }else{ Toast.makeText(MainActivity.this, "没有菜品了", Toast.LENGTH_SHORT).show(); mImageFood.setImageResource(R.drawable.ic_launcher_foreground); } } else { if (mCurrentFoodIndex < mFoodsResult.size()) { Toast.makeText(MainActivity.this, "菜品:" + mFoodsResult.get(mCurrentFoodIndex).getName() + "姓名:" + mPerson.getName() + "性别:" + mPerson.getSex(), Toast.LENGTH_SHORT).show(); } } } }); } private void findFood() { if (mFoodsResult == null) { mFoodsResult = new ArrayList<>(); } mShowToggleButton.setChecked(true); mFoodsResult.clear(); for (int i = 0; i < mFoods.size(); i++) { Food food = mFoods.get(i); if(food.getPrice() < mPrice && ((food.isHot() == mHot && food.isHot()) || (food.isFish() == mFish && food.isFish()) || (food.isSoul() == mSoul && food.isSoul()) || (food.isFish() == mFish && food.isHot() == mHot && food.isSoul() == mSoul))) { mFoodsResult.add(food); } } mCurrentFoodIndex = 0; if (mFoodsResult.size() > mCurrentFoodIndex) { mImageFood.setImageResource(mFoodsResult.get(mCurrentFoodIndex).getPic()); } else{ mImageFood.setImageResource(R.drawable.ic_launcher_foreground); Toast.makeText(MainActivity.this, "没有菜品了", Toast.LENGTH_SHORT).show(); } } public void initData() { 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(); mPerson.setSex("男"); } public void init() { mEditName = findViewById(R.id.mEditName); mRadioSex = findViewById(R.id.mRadioSex); mIsHotCheck = findViewById(R.id.isHotCheck); mIsFishCheck = findViewById(R.id.isFishCheck); mIsSoulCheck = findViewById(R.id.isSoulCheck); mPriceSeekBar = findViewById(R.id.PriceSeekBar); mSelectFood = findViewById(R.id.selectFood); mImageFood = findViewById(R.id.imageFood); mShowToggleButton = findViewById(R.id.showToggleButton); } }
Food.java 属性(名字,价格,图片id, 辣,海鲜,酸)
package com.example.ofoodselection; public class Food { private String name; private int price; private int pic; private boolean hot; private boolean fish; private boolean soul; public Food(String name, int price, int pic, boolean hot, boolean fish, boolean soul) { this.name = name; this.price = price; this.pic = pic; this.hot = hot; this.fish = fish; this.soul = soul; } 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 isSoul() { return soul; } public void setSoul(boolean soul) { this.soul = soul; } }
Person.java 属性 名字 性别
package com.example.ofoodselection; 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; } }