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的方法
}
}
相關文章
- android基礎學習-android篇day12-UI基礎控制元件(上)AndroidUI控制元件
- android基礎學習-android篇day13-UI基礎控制元件(下)AndroidUI控制元件
- Android基礎學習Android
- android基礎學習-java篇day11-綜合測試-播放器管理系統AndroidJava播放器
- android基礎學習-android篇day12-android的UI基礎入門AndroidUI
- Android 面試基礎篇Android面試
- Android基礎知識學習Android
- Android自定義控制元件系列之基礎篇Android控制元件
- android基礎學習-android篇day16-Menu的使用Android
- Dart基礎系統學習Dart
- android基礎學習-android篇day17-Android Fragment(碎片)基本使用AndroidFragment
- android基礎學習-android篇day16-Dialog的使用Android
- Android NDK學習筆記1-基礎知識篇Android筆記
- Java基礎 --- 綜合練習Java
- Android 動畫基礎知識學習(下)Android動畫
- Android面試之Java 基礎篇Android面試Java
- Linux系統基礎學習Linux
- Android基礎Android
- Python基礎學習篇Python
- 如何系統學習C 語言(上)之 基礎篇
- android基礎學習-android篇day17-Activity的生命週期(轉)Android
- android基礎學習-android篇day11-android的入門工具安裝流程Android
- Android基礎—FragmentAndroidFragment
- MySQL學習筆記【基礎篇】MySql筆記
- JAVA基礎學習篇之反射Java反射
- javaScript學習基礎篇(3)(字串)JavaScript字串
- MySQL學習基礎之起航篇MySql
- bootstrap基礎學習一篇boot
- 聚焦傳統網路,學習SDN基礎和案例
- 零基礎學前端之CSS文字與字型綜合案例前端CSS
- MS(2):Android之基礎知識篇Android
- 系統學習 TypeScript(三)——基礎型別TypeScript型別
- 身為學霸的我學習linux系統之基礎篇Linux
- android基礎學習-android篇day15-相對佈局的常用屬性Android
- Android顯示子系統相關基礎概念Android
- Linux基礎學習五點Linux
- Apple Watch學習之路 基礎控制元件學習APP控制元件
- Android基礎知識Android