综合案例:选餐
课程介绍及基础控件
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>
-
CheckBox
-
RadioButton
-
ToggleButton
-
SeekBar
选餐案例实现
- 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>
- 准备工作
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;
}
}
添加事件监听
-
查询菜品
-
显示菜品