欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

安卓学习---复习巩固

程序员文章站 2022-04-16 16:21:07
本次主要针对以前学习的内容进行复习巩固,虽然也学习安卓两个月了,但 是感觉对于一些控件和布局的使用依然不是那么熟练,这一次主要是通过一个选餐的Demo进行复习巩固内容首先是布局文件:?xml version="1.0" encoding="utf-8"?>
    本次主要针对以前学习的内容进行复习巩固,虽然也学习安卓两个月了,但 是感觉对于一些控件和布局的使用依然不是那么熟练,这一次主要是通过一个选餐的Demo进行复习巩固

内容

首先是布局文件:


?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="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">

<!--在顶部正中间设置一标题-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#B9B9FF"
        android:text="开始选餐"
        android:textSize="30sp"
        android:textAlignment="center"
        android:textColor="#8A2BE2"
        android:textStyle="bold|italic"
        android:typeface="monospace"
        android:layout_marginBottom="10dp"
        />

<!--最外层的布局为了将整个屏幕的区域平分为两个部分-->
<LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="0dp"
        android:layout_weight="1">

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

<!--输入姓名-->
            <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_marginLeft="15dp"
            android:layout_width="match_parent"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:layout_height="45dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="性别"
                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:layout_width="75dp"
                    android:layout_height="wrap_content"
                    android:text="男"
                    android:textSize="22sp"/>

                <RadioButton
                    android:id="@+id/femaleRadioButton"
                    android:layout_width="75dp"
                    android:layout_height="wrap_content"
                    android:text="女"
                    android:textSize="22sp"/>
            </RadioGroup>
        </LinearLayout>
        
<!--这两部分的勾选是不同的,起初本来都是想用CheckBox去实现的,但学习过程中又发现了RadioGroup控件,索性就把这个也学习了一遍,发现这两个控件的区别还是蛮大的。区别:
1.RadioButton点击选中后无法再次点击取消选中,而CheckBox可以实现
2.RadioButton不能同时点击选中多个,而CheckBox可选中多个
-->

 <!--设置口味的喜好勾选框-->
        <LinearLayout
            android:layout_marginLeft="15dp"
            android:layout_width="match_parent"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:layout_height="45dp">

            <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_marginLeft="15dp"
            android:layout_width="match_parent"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:layout_height="45dp">

            <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控件实现选择的滑动效果-->
                <SeekBar
                    android:id="@+id/seekBar"
                    android:layout_width="230dp"
                    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按钮----点击"寻找菜品"显示相应的菜品-->
        <Button
            android:id="@+id/searchButton"
            android:layout_width="300dp"
            android:layout_height="100dp"
            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_background"
            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:layout_width="300dp"
            android:layout_height="50dp"/>

    </LinearLayout>

</LinearLayout>

MainActivity

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<Food> mFoods;
    private Person mPerson;
    private List<Food> mFoodResult;

    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_main);

        //初始化控件
        findViews();

        //初始化数据
        initData();

        //为控件添加监听器,实现基本功能
        setListeners();

        //自测

    }

    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 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.guilinmifen, false, false, false));
        mFoods.add(new Food("上汤娃娃菜", 28, R.drawable.shangtangwawacai, 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.suancainiutoumian, false, false, false));
        mFoods.add(new Food("西芹炒百合", 38, R.drawable.xiqinchaobaihe, false, false, false));
        mFoods.add(new Food("酸辣汤", 40, R.drawable.suanlatang, true, false, true));

        mPerson = new Person();
        mFoodResult = new ArrayList<>();
    }

    private void setListeners(){
        //设置单选框
        //RadioGroupListener radioGroupListener = new RadioGroupListener();
        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;
                }
            }
        });

        //设置复选框
        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() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //int price = seekBar.getProgress();
                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 < mFoodResult.size()){
                        mFoodImageView.setImageResource(mFoodResult.get(mCurrentIndex).getPic());
                    }
                } else{
                    if (mCurrentIndex < mFoodResult.size()) {
                        String foodNmae = mFoodResult.get(mCurrentIndex).getName();
                        String personName = mNameEditText.getText().toString();
                        String sex = mPerson.getSex();
                        Toast.makeText(MainActivity.this, "菜名: " + foodNmae + "\n客户: " + personName + "\n性别: " + sex, 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());
        }
    }


}

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

运行结果:
安卓学习---复习巩固
安卓学习---复习巩固

安卓学习---复习巩固
安卓学习---复习巩固

总结

这一次的Demo练习收获不少,熟悉了不少,同时也意识到自己的Java也需要加快进度去学习,理清Activity的实现过程。下一阶段将进一步巩固数据存储以及网络编程部分。

本文地址:https://blog.csdn.net/qq_45921336/article/details/107484399