Android Studio 实验三:水果店

目录

activity_main.xml

Activity

Fruit和Person类

 Fruit

 Person

 运行截图

项目文件


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    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_marginBottom="10dp"
        android:background="#B9B9FF"
        android:text="@string/start_select_fruit"
        android:textColor="#8A2BE2"
        android:textSize="20sp"
        android:textStyle="bold|italic"
        android:typeface="monospace"
        android:gravity="center"
        /><!--标题-->

    <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="30dp"
            android:layout_marginLeft="15dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">

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

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

        </LinearLayout><!--姓名-->

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

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

            <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="15sp"
                    android:layout_width="75dp"
                    android:layout_height="wrap_content"
                    android:text="@string/male"/>

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

                <RadioButton
                    android:id="@+id/otherRadioButton"
                    android:textSize="15sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/other"
                    />
            </RadioGroup>

        </LinearLayout><!--性别-->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            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="15sp"
                />

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

                <RadioButton
                    android:id="@+id/humankind"
                    android:textSize="15sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="人类种"/>

                <RadioButton
                    android:id="@+id/animalkind"
                    android:textSize="15sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="亚人种"/>

                <RadioButton
                    android:id="@+id/tanjikind"
                    android:textSize="15sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="非碳基生物"/>

            </RadioGroup>

        </LinearLayout><!--人种-->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            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="15sp"/>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <CheckBox
                    android:id="@+id/tropicalCheckBox"
                    android:text="热带水果"
                    android:textSize="15sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <CheckBox
                    android:id="@+id/subtropicalCheckBox"
                    android:text="亚热带水果"
                    android:textSize="15sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <CheckBox
                    android:id="@+id/temperateCheckBox"
                    android:text="温带水果"
                    android:textSize="15sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>

            </LinearLayout>

        </LinearLayout><!--分类-->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="25dp"
            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="15sp"/>

            <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="15sp"/>
                <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="15sp"/>

            </LinearLayout>

        </LinearLayout><!--预算-->

        <Button
            android:id="@+id/searchButton"
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:text="寻找水果"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:textSize="15sp"/><!--寻找水果按钮-->

    </LinearLayout>

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

        <ImageView
            android:id="@+id/fruitsImageView"
            android:src="@mipmap/fresh_fruit"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"/>
        <ToggleButton
            android:id="@+id/showToggleButton"
            android:textOff="下一个"
            android:textOn="显示信息"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:textSize="15sp"
            android:layout_width="300dp"
            android:layout_height="40dp"/>
    </LinearLayout><!--显示图片和按钮-->


</LinearLayout>






Activity

public class MainActivity extends AppCompatActivity{

    private EditText mNameEditText;
    private RadioGroup mSexRadioGroup;
    private CheckBox tropicalCheckBox, subtropicalCheckBox, temperateCheckBox;
    private SeekBar mSeekBar;
    private Button mSearchButton;
    private ImageView fruitImageView;
    private ToggleButton mToggleButton;
    private List<Fruit> fruits;
    private Person mPerson;
    private List<Fruit> fruitdResults;
    private boolean isTropical;
    private boolean isSubtropical;
    private boolean isTemperate;
    private int mPrice;
    private int mCurrentIndex;

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

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

    }

    private void initData() {
        // new 出来一个空的水果 list
        fruits = new ArrayList<>();

        // 初始化添加所有的数据
        // 热带水果
        fruits.add(new Fruit("柠檬", 55, R.mipmap.lenmo, true, false, false));
        fruits.add(new Fruit("芒果", 80, R.mipmap.mango, true, false, false));
        fruits.add(new Fruit("菠萝", 68, R.mipmap.pineapple, true, false, false));
        // 亚热带水果
        fruits.add(new Fruit("荔枝", 43, R.mipmap.litchi, false, true, false));
        fruits.add(new Fruit("杨梅", 34, R.mipmap.waxberry, false, true, false));
        fruits.add(new Fruit("杨桃", 22, R.mipmap.carambola, false, true, false));
        // 温带水果
        fruits.add(new Fruit("桃子", 58, R.mipmap.peach, false, false, true));
        fruits.add(new Fruit("李子", 74, R.mipmap.plum, false, false, true));
        fruits.add(new Fruit("柿子", 12, R.mipmap.persimmon, false, false, true));
        mPerson = new Person();

        fruitdResults = new ArrayList<>();
    }

    private void findViews() {
        mNameEditText = findViewById(R.id.nameEditText);
        mSexRadioGroup = findViewById(R.id.sexRadioGroup);
        tropicalCheckBox = findViewById(R.id.tropicalCheckBox);
        subtropicalCheckBox = findViewById(R.id.subtropicalCheckBox);
        temperateCheckBox = findViewById(R.id.temperateCheckBox);
        mSeekBar = findViewById(R.id.seekBar);
        mSeekBar.setProgress(30);
        mSearchButton = findViewById(R.id.searchButton);
        mToggleButton = findViewById(R.id.showToggleButton);
        mToggleButton.setChecked(true);
        fruitImageView = findViewById(R.id.fruitsImageView);
    }

    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
        tropicalCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // 检测复选框是否选中
                isTropical = isChecked;
            }
        });
        subtropicalCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isSubtropical = isChecked;
            }
        });
        temperateCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isTemperate = isChecked;
            }
        });

        // 设置seekBar的监听
        mSeekBar.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();
            }
        });

        mSearchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search();
            }
        });

        mToggleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mToggleButton.isChecked()) {
                    //  下一个
                    mCurrentIndex++;
                    if (mCurrentIndex < fruitdResults.size()) {
                        // 改变当前图片资源
                        fruitImageView.setImageResource(fruitdResults.get(mCurrentIndex).getPic());
                    } else {
                        Toast.makeText(MainActivity.this, "没有啦", Toast.LENGTH_SHORT).show();

                    }
                } else {
                    // 显示信息:水果的名称
                    if (mCurrentIndex < fruitdResults.size()) {
                        Toast.makeText(MainActivity.this, "水果名: " + fruitdResults.get(mCurrentIndex).getName(), Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, "没有啦", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    // 查找水果
    private void search() {
        // 结果列表每次都清空
        // 遍历所有水果
        // 如果符合条件,则加入到我们的结果列表中

        // 如果为空,先初始化
        if (fruitdResults == null) {
            fruitdResults = new ArrayList<>();
        }

        // 先清除之前的结果
        fruitdResults.clear();
        // 当前显示的是结果中的第几个水果
        mCurrentIndex = 0;

        for (int index = 0; index < fruits.size(); index++) {
            Fruit fruit = fruits.get(index);
            if (fruit != null) {
                // 价格要小于设定的价格
                // 判断是否是顾客选择的口味

                if (fruit.getPrice() < mPrice) {
                    // 添加热带水果
                    if (isTropical) {
                        if (fruit.isTropical() == isTropical) {
                            fruitdResults.add(fruit);
                        }
                    }
                    // 添加亚热带水果
                    if (isSubtropical) {
                        if (fruit.isSubtropical() == isSubtropical) {
                            fruitdResults.add(fruit);
                        }
                    }
                    // 添加温带水果
                    if (isTemperate) {
                        if (fruit.isTemperate() == isTemperate) {
                            fruitdResults.add(fruit);
                        }
                    }


                }
            }
        }

        // 先显示第一张图片
        if (mCurrentIndex < fruitdResults.size()) {
            // 如果list有值,将显示图片换成当前的
            fruitImageView.setImageResource(fruitdResults.get(mCurrentIndex).getPic());
        } else {
            // 如果list没有值,显示默认图片
            fruitImageView.setImageResource(R.mipmap.fresh_fruit);
        }
    }

}

Fruit和Person类

创建一个Package

命名为model,回车确定

在这个包类创建两个Java类,一个命名为Fruit,一个命名为Person

 Fruit

public class Fruit {
    // 水果名称
    private String name;
    // 水果价格
    private double price;
    // 水果图片路径
    private int pic;
    // 是否是热带水果
    private boolean isTropical;
    // 是否是亚热带水果
    private boolean isSubtropical;
    // 是否是温带水果
    private boolean isTemperate;

    public Fruit(String name, double price, int pic, boolean isTropical, boolean isSubtropical, boolean isTemperate) {
        this.name = name;
        this.price = price;
        this.pic = pic;
        this.isTropical = isTropical;
        this.isSubtropical = isSubtropical;
        this.isTemperate = isTemperate;
    }

    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getPic() {
        return pic;
    }

    public void setPic(int pic) {
        this.pic = pic;
    }

    public boolean isTropical() {
        return isTropical;
    }

    public void setTropical(boolean tropical) {
        isTropical = tropical;
    }

    public boolean isSubtropical() {
        return isSubtropical;
    }

    public void setSubtropical(boolean subtropical) {
        isSubtropical = subtropical;
    }

    public boolean isTemperate() {
        return isTemperate;
    }

    public void setTemperate(boolean temperate) {
        isTemperate = temperate;
    }
}

 Person

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

}

 运行截图

因为排版问题,EditText好像写不了字,不过问题不大。

项目文件

https://download.csdn.net/download/nazonomaster/85219410icon-default.png?t=M3K6https://download.csdn.net/download/nazonomaster/85219410

 

 

  • 0
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nazonomaster

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值