笔记
综合案例:选餐
下面展示一些 内联代码片
。
package com.example.myapplication;
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 com.example.myapplication.model.Food;
import com.example.myapplication.model.Person;
import java.util.ArrayList;
import java.util.List;
public class MainActivity2 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 Person mPerson;
private List<Food> mFoodResult;
private List<Food> mFoods;
private boolean mIsFish;
private boolean mIsSour;
private boolean mIsHot;
private int mprice;
private int mCurrentIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//初始化控件
findViews();
//初始化数据
initDate();
//为控件添加监听器,实现基本功能
setListeners();
//自测
}
private void setListeners() {
//editText加入了监听
mNameEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (mPerson != null) {
mPerson.setName(editable.toString());
}
}
});
//设置单选框
mSexRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.maleRadioButton:
mPerson.setSex("男");
break;
case R.id.femaleRadioButton:
mPerson.setSex("女");
break;
}
}
});
//设置复选框
mfishCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
mIsFish = b;
}
});
mhotCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
mIsHot = b;
}
});
msourCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
mIsSour = b;
}
});
mseekBar.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) {
mprice = seekBar.getProgress();
Toast.makeText(MainActivity2.this, "价格" + mprice, Toast.LENGTH_SHORT).show();
}
});
msearchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
search();
}
});
mToggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mToggleButton.isChecked()) {
//下一个菜
mCurrentIndex++;
if (mCurrentIndex < mFoodResult.size()) {
mFoodImageView.setImageResource(mFoodResult.get(mCurrentIndex).getPic());
} else {
Toast.makeText(MainActivity2.this, "没有了", Toast.LENGTH_SHORT).show();
}
} else {
//显示信息:菜的名称
if (mCurrentIndex < mFoodResult.size()) {
String foodName = mFoodResult.get(mCurrentIndex).getName();
// String personName = mNameEditText.getText().toString();
String personName = mPerson.getName();
String sex = mPerson.getSex();
Toast.makeText(MainActivity2.this, "菜名" + foodName + ",人名" + personName + ",性别" + sex, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity2.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() || food.isFish() || food.isSour())) {
mFoodResult.add(food);
}
}
}
if (mCurrentIndex < mFoodResult.size()) {
mFoodImageView.setImageResource(mFoodResult.get(mCurrentIndex).getPic());
}
}
private void initDate() {
//new 出来一个空的食物list
mFoods = new ArrayList<>();
mFoods.add(new Food("x01", 55, R.mipmap.x01, true, false, false));
mFoods.add(new Food("x02", 48, R.mipmap.x02, true, true, false));
mFoods.add(new Food("x03", 80, R.mipmap.x03, true, true, false));
mFoods.add(new Food("x04", 68, R.mipmap.x04, false, true, false));
mFoods.add(new Food("x05", 15, R.mipmap.x05, true, false, false));
mFoods.add(new Food("x06", 28, R.mipmap.x06, true, false, false));
mFoods.add(new Food("x07", 60, R.mipmap.x07, true, false, false));
mFoods.add(new Food("x08", 40, R.mipmap.x08, true, false, false));
mFoods.add(new Food("x09", 35, R.mipmap.x09, true, false, false));
mFoods.add(new Food("x10", 38, R.mipmap.x10, true, false, false));
mFoods.add(new Food("x11", 40, R.mipmap.x11, 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.ToggleButton);
mToggleButton.setChecked(true);
mFoodImageView = findViewById(R.id.FoodImageView);
}
}
<?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=".MainActivity2">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#B9B9FF"
android:text="开始选餐"
android:textAlignment="center"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="monospace" />
<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="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" />
<EditText
android:id="@+id/nameEditText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:hint="请输入姓名" />
</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" />
<RadioGroup
android:id="@+id/SexRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/maleRadioButton"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:text="@string/male"
android:textSize="22sp" />
<RadioButton
android:id="@+id/femaleRadioButton"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:text="@string/female"
android:textSize="22sp" />
</RadioGroup>
</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" />
<CheckBox
android:id="@+id/hotCheckBox"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:text="辣"
android:textSize="22sp" />
<CheckBox
android:id="@+id/fishCheckBox"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="海鲜"
android:textSize="22sp" />
<CheckBox
android:id="@+id/sourCheckBox"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:text="酸"
android:textSize="22sp" />
</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" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0元"
android:textSize="22sp" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:max="100" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100元"
android:textSize="22sp" />
</LinearLayout>
<Button
android:id="@+id/searchButton"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="寻找菜品"
android:textSize="22sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/FoodImageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:src="@mipmap/ic_launcher" />
<ToggleButton
android:id="@+id/ToggleButton"
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:gravity="center"
android:textOff="下一个"
android:textOn="显示信息"
android:textSize="22sp" />
</LinearLayout>
</LinearLayout>
package com.example.myapplication.model;
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;
}
}
package com.example.myapplication.model;
public class Person {
private String name;
private String sex;
private Food food;
public Person() {
super();
}
public Person(String name, String sex, Food food) {
this.name = name;
this.sex = sex;
this.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;
}
}