android基础学习-android篇day14-UI基础控件综合案例——点餐系统

案例分析:

一、资源

  • 图片资源:各种菜品图片(后缀jpg或png),放置到res/drawable文件夹下

二、XML布局文件

将布局分成三部分

  • a:Title—>"选餐Start!"<—TextView
  • b:寻找由姓名(TextView)开始至寻找(Button)
  • 垂直方向的线性布局*weight=1
  • 每一行添加不同的控件:TextView...
  • 最后一行添加Button
  • *需要为每一个在Java代码中使用的的控件添加id属性
  • c:显示—>由图片(ImageView)开始至显示(ToggleButton)
  • 垂直方向的线性布局,*weight=1;
  • 第一行ImageView
  • 第二行ToggleButton

三、java代码

  • a:initView();初始化控件
  • b:initData();初始化数据
  • c:setLisstener();为控件添加监听器
  • 1、寻找按钮
  • 1)元素数据通过List<object>保存(创建一个实体类用于存放菜品)
  • 2)筛选出数据通过List<object>保存
  • if(预算之下){
  • if(满足第三个CheckBox状态)
  • {将1)负荷条件的数据添加到2中)中
  • 在ImageView中显示图片}
  • }
  • 2、SHOW/NEXT(显示信息/下一个)
  • 1)SHOW
  • 显示图片信息(List)
  • 2)NEXT如果当前List还有数据便显示下一个图片,如果没有显示回到第一图片

activity_main.xml

<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" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="选餐Start"
        android:textColor="@color/blueviolet"
        android:textSize="25sp"
        android:textStyle="bold|italic"
        android:typeface="monospace" 
        android:background="@color/yellow"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical" 
        android:layout_weight="1"
        android:background="@color/aliceblue">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="姓名" />

            <EditText
                android:id="@+id/et"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="请输入姓名" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="性别" />

            <RadioGroup
                android:id="@+id/rg"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <RadioButton
                    android:id="@+id/rb_male"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="男" />

                <RadioButton
                    android:id="@+id/rb_female"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="女" />
            </RadioGroup>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="喜好" />

            <CheckBox
                android:id="@+id/cb_hot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="辣" />

            <CheckBox
                android:id="@+id/cb_fish"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="海鲜" />

            <CheckBox
                android:id="@+id/cb_sour"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="酸" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="预算" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="right"
                android:text="0元" />

            <SeekBar
                android:id="@+id/sb"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:max="100" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="100元" />
        </LinearLayout>

        <Button
            android:id="@+id/bt_find"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@color/blueviolet"
            android:text="寻找菜品" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical" 
          android:layout_weight="1"
          android:background="@color/aqua">

     
            <ImageView 
                android:id="@+id/iv_pic"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:src="@drawable/ic_launcher"/>
            <ToggleButton 
                android:id="@+id/tb_click"
                  android:layout_width="match_parent"
                android:layout_height="0dp"
                android:textOn="下一个"
                android:textOff="显示信息"
                 android:layout_weight="1" 
                 android:background="@color/bisque"
                 android:layout_margin="5dp"
                
                />
    </LinearLayout>

</LinearLayout>

Food.java//食物的封装类

package com.example.base_tsetdemo;

public class Food {
	public Food(String name,int price, int pic, boolean hot, boolean fish, boolean sour
			) {
		super();
		this.name = name;
		this.hot = hot;
		this.fish = fish;
		this.sour = sour;
		this.price = price;
		this.pic = pic;
	}

	private String name;
	private boolean hot;
	private boolean fish;
	private boolean sour;
	private int price;
	private int pic;

	public String getName() {
		return name;
	}

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

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

	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 String toString(){
		return "菜名:"+this.getName()+"-"+"价格:"+this.getPrice()+"-"+"海鲜"+isFish()+"-"+"辣"+isHot()+"-"+"酸"+isSour();
		
	}
}

Person.java个人信息的封装类

package com.example.base_tsetdemo;

public class Person {

	private String name;
	private String sex;
	Food food;
	public Food getFood() {
		return food;
	}
	public void setFood(Food food) {
		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 String toString(){
		return "姓名:"+this.getName()+"-"+"性别:"+this.getSex()+"-"+"菜:"+this.getFood();
	}
	
}

MainActivity.java

package com.example.base_tsetdemo;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
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.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;

/*步骤
 * 1、初始化控件
 * 2、初始化数据
 * 3、为控件添加监听器
 * 
 */

public class MainActivity extends ActionBarActivity {
	private EditText name;
	private RadioGroup sex;
	private CheckBox hot, fish, sour;
	private Button find;
	private SeekBar seekbar;
	private ImageView iv_pic;
	private ToggleButton togglebutton;
	private List<Food> lists_food;// 初始化的List
	private List<Food> lists_get;// 查找的list
	private Person person;
	private RadioGroupListener radiogrouplistener;
	private CheckBoxListener checkboxlistener;
	private SeekBarListener seekbarlistener;
	private ButtonListener buttonlistener;
	private boolean isFish, isSour, isHot;
	private int price = 30;
	private int i = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();// 初始化控件
		initData();// 初始化数据
		// 为添加监听器
		setListener();

	}

	private void setListener() {
		radiogrouplistener = new RadioGroupListener();
		// 为当前单选按钮添加监听器
		sex.setOnCheckedChangeListener(radiogrouplistener);
		// 为checkbox添加监听器
		checkboxlistener = new CheckBoxListener();
		fish.setOnCheckedChangeListener(checkboxlistener);
		sour.setOnCheckedChangeListener(checkboxlistener);
		hot.setOnCheckedChangeListener(checkboxlistener);
		// 为seekbar添加监听器
		seekbarlistener = new SeekBarListener();
		seekbar.setOnSeekBarChangeListener(seekbarlistener);
		// 为button寻找按键添加监听器
		buttonlistener = new ButtonListener();
		find.setOnClickListener(buttonlistener);
		togglebutton.setOnClickListener(buttonlistener);

	}

	private void initData() {// 初始化数据
		person = new Person();
		lists_food = new ArrayList<Food>();
		lists_get = new ArrayList<Food>();
		lists_food.add(new Food("麻辣香锅", 55, R.drawable.malaxiangguo, true,
				false, false));

		lists_food.add(new Food("水煮鱼", 48, R.drawable.shuizhuyu, true, true,
				false));
		lists_food.add(new Food("麻辣火锅", 80, R.drawable.malahuoguo, true, true,
				false));

		lists_food.add(new Food("清蒸鲈鱼", 68, R.drawable.qingzhengluyu, false,
				true, false));

		lists_food.add(new Food("桂林米粉", 15, R.drawable.guilin, false, false,
				false));
		lists_food.add(new Food("上汤娃娃菜", 28, R.drawable.wawacai, false, false,
				false));
		lists_food.add(new Food("红烧肉", 60, R.drawable.hongshaorou, false,
				false, false));
		lists_food.add(new Food("木须肉", 40, R.drawable.muxurou, false, false,
				false));
		lists_food.add(new Food("酸菜牛肉面", 35, R.drawable.suncainiuroumian,
				false, false, true));
		lists_food.add(new Food("西芹炒百合", 38, R.drawable.xiqin, false, false,
				false));

		lists_food.add(new Food("酸辣汤", 40, R.drawable.suanlatang, true, false,
				true));

	}

	private void initView() {
		name = (EditText) findViewById(R.id.et);
		sex = (RadioGroup) findViewById(R.id.rg);
		hot = (CheckBox) findViewById(R.id.cb_hot);
		fish = (CheckBox) findViewById(R.id.cb_fish);
		sour = (CheckBox) findViewById(R.id.cb_sour);
		seekbar = (SeekBar) findViewById(R.id.sb);
		iv_pic = (ImageView) findViewById(R.id.iv_pic);
		seekbar.setProgress(30);// 进度默认值
		find = (Button) findViewById(R.id.bt_find);
		togglebutton = (ToggleButton) findViewById(R.id.tb_click);
		name.addTextChangedListener(new EditTextListener());
	}

	// 输入姓名的监听
	class EditTextListener implements TextWatcher {

		@Override
		public void afterTextChanged(Editable arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			Log.i("name", arg0.toString());
			person.setName(arg0.toString());

		}

	}

	class RadioGroupListener implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(RadioGroup arg0, int arg1) {
			// 当用户选择当前RadioGroup组成的button时被触发
			switch (arg1) {
			case R.id.rb_male:
				person.setSex("男");
				break;
			case R.id.rb_female:
				person.setSex("女");
				break;
			default:
				break;
			}
			Log.i("sex", person.getSex());

		}

	}

	class CheckBoxListener implements
			android.widget.CompoundButton.OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
			// 当控件状态改变时触发
			CheckBox cbox = (CheckBox) arg0;
			switch (cbox.getId()) {
			case R.id.cb_fish:
				if (arg1) {
					isFish = true;
				} else {
					isFish = false;
				}

				break;
			case R.id.cb_hot:
				if (arg1) {
					isHot = true;
				} else {
					isHot = false;
				}

				break;
			case R.id.cb_sour:
				if (arg1) {
					isSour = true;
				} else {
					isSour = false;
				}

				break;

			default:
				break;
			}
			Log.i("sex", "海鲜" + isFish + "辣" + isHot + "酸" + isSour);

		}

	}

	// seekbar监听函数
	class SeekBarListener implements OnSeekBarChangeListener {

		@Override
		public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStartTrackingTouch(SeekBar arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStopTrackingTouch(SeekBar arg0) {
			// TODO Auto-generated method stub
			price = seekbar.getProgress();// 获取当前默认值
			Toast.makeText(MainActivity.this, "价格:" + price, Toast.LENGTH_SHORT)
					.show();

		}

	}

	// 寻找的监听
	class ButtonListener implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.bt_find:
				// 当用户点击寻找时,需要帅选信息,并显示到TextView上
				checkData();
				break;
			case R.id.tb_click:
				// 选中状态和未选中状态
				if (!togglebutton.isChecked())// 如果是选中状态
				{

					for (int i = 0; i < lists_get.size(); i++) {
						// showPic(i);
						// showPic(i);
						Food food = lists_get.get(i);
						Person perosn = new Person();
						person.setFood(food);// 封装到person中
						Toast.makeText(MainActivity.this, "" + person,
								Toast.LENGTH_SHORT).show();
						// Log.i("id",
						// Integer.toString(lists_get.get(i).getPic()));

					}

				} else {
					if (lists_get.isEmpty()) {
						Toast.makeText(MainActivity.this, "" + "没有信息",
								Toast.LENGTH_SHORT).show();
					} else {

						if (lists_get.size() > i) {
							// showPic(i);
							// Food food=lists_get.get(i);
							// Person perosn=new Person();
							showPic(i);// 封装到person中
							Log.i("id", "第" + i + "个" + "-" + "数组长度"
									+ lists_get.size());
							i++;
							break;
						} else {
							Toast.makeText(MainActivity.this, "到末尾啦",
									Toast.LENGTH_SHORT).show();
						}
					}

					// Log.i("id", Integer.toString(lists_get.get(i).getPic()));

				}

				break;

			default:
				break;
			}

		}

		private void checkData() {
			// 找出菜品
			for (int i = 0; i < lists_food.size(); i++) {
				Food food = lists_food.get(i);
				if (food.getPrice() <= price && food.isFish() == isFish
						&& food.isHot() == isHot && food.isSour() == isSour) {
					lists_get.add(food);
					Log.i("find", Integer.toString(lists_get.size()));

				}

			}

		}

		// 显示菜品图片
		private void showPic(int count) {

			iv_pic.setImageResource(lists_get.get(count).getPic());
		}
		// toggleButton的方法

	}
}

猜你喜欢

转载自blog.csdn.net/qq_17846019/article/details/82773651