【无标题】

综合案例:选餐

课程介绍及基础控件

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

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

        CheckBox checkBox = findViewById(R.id.checkBox);
        //checkBox.setChecked(true);
        boolean isChecked = checkBox.isChecked();


        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                Log.d(TAG, "onCheckedChanged: " + isChecked);
            }
        });

        SeekBar seekBar = findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
                Log.d(TAG, "onProgressChanged: " + progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Log.d(TAG, "onStartTrackingChanged: " + seekBar.getProgress());
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Log.d(TAG, "onStopTrackingChanged: " + seekBar.getProgress());
            }
        });
    }
}
<?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">


    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="CheckBox" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="RadioButton" />

    </RadioGroup>

    <ToggleButton
        android:id="@+id/toggleButton2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="ToggleButton"
        android:textOff="文豪"
        android:textOn="郗" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30" />


</LinearLayout>
  1. CheckBox
    在这里插入图片描述
    在这里插入图片描述

  2. RadioButton
    在这里插入图片描述

  3. ToggleButton
    在这里插入图片描述

  4. SeekBar
    在这里插入图片描述

选餐案例实现

在这里插入图片描述

  1. UI控件
<?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:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RadioButton
                    android:layout_width="75dp"
                    android:layout_height="wrap_content"
                    android:text="@string/male"
                    android:textSize="22sp" />

                <RadioButton
                    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:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="5"
            android:src="@mipmap/ic_launcher" />

        <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>
  1. 准备工作
    在这里插入图片描述
    在这里插入图片描述
package com.example.myapplication.model;

public class Food {
    private String name;
    private String price;
    private int pic;
    private boolean hot;
    private boolean fish;
    private boolean sour;

    public Food(String name, String 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 String getPrice() {
        return price;
    }

    public void setPrice(String 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;
    }
}

在这里插入图片描述

添加事件监听

  1. 查询菜品
    在这里插入图片描述在这里插入图片描述在这里插入图片描述

  2. 显示菜品

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值