要求:拖动预算条能够显示价格,也能根据口味和价格查询菜品,点击下一页时提示菜品和用户名
文件结构
代码如下
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>
演示