实现咖啡选择
<?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">
<TextView
android:id="@+id/coffee_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="卡布奇诺"
android:layout_gravity="center"
android:layout_marginLeft="0dp"
style="@style/coffee"/>
<ImageView
android:id="@+id/iv_coffee"
android:layout_width="match_parent"
android:layout_height="309dp"
android:src="@drawable/c" />
<TextView
android:id="@+id/coffee_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数量:20"
style="@style/coffee"/>
<TextView
android:id="@+id/coffee_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="价格:26元"
style="@style/coffee"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="描述:"
style="@style/coffee"/>
<TextView
android:id="@+id/coffee_describe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="卡布奇诺是一种很好喝的咖啡!"
style="@style/coffee"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="20dp">
<Button
android:id="@+id/kabuqinuo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="卡布奇诺"
android:textSize="25sp" />
<Button
android:id="@+id/natie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="拿铁" />
<Button
android:id="@+id/meishikafei"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="美式咖啡"
android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
package xdglyt.software.jhwz.myapplication11;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView cf_name;
private TextView cf_price;
private TextView cf_number;
private TextView cf_describe;
private Button btn_mskf;
private Button btn_nt;
private Button btn_kbqn;
private ImageView iv_coffee;
private Map<String,String>map;
private List<Map<String,String>> list;
private String name,price,number,describe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_coffee = (ImageView)findViewById(R.id.iv_coffee);
cf_describe =(TextView) findViewById(R.id.coffee_describe);
cf_name =(TextView) findViewById(R.id.coffee_name);
cf_price =(TextView) findViewById(R.id.coffee_price);
cf_number =(TextView) findViewById(R.id.coffee_number);
btn_kbqn =(Button) findViewById(R.id.kabuqinuo);
btn_mskf =(Button) findViewById(R.id.meishikafei);
btn_nt =(Button) findViewById(R.id.natie);
btn_kbqn.setOnClickListener(this);
btn_nt.setOnClickListener(this);
btn_mskf.setOnClickListener(this);
try{
InputStream inputStream = this.getResources().openRawResource(R.raw.coffee1);
List<Coffee>coffeeInfos=CoffeeService.getInfosFromXML(inputStream);
list = new ArrayList<Map<String,String>>();
for(Coffee coffee:coffeeInfos){
map = new HashMap<String, String>();
map.put("name",coffee.getCoffeeName());
map.put("price",coffee.getCoffeePrice());
map.put("describe",coffee.getCoffeeDescribe());
map.put("number",coffee.getCoffeeNumber());
list.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
getMap(1,R.drawable.a);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.kabuqinuo:
getMap(0,R.drawable.a);
break;
case R.id.meishikafei:
getMap(1,R.drawable.b);
break;
case R.id.natie:
getMap(2,R.drawable.c);
break;
}
}
private void getMap(int i, int a) {
Map<String,String> coffeeMap = list.get(i);
name = coffeeMap.get("name");
price = coffeeMap.get("price");
number = coffeeMap.get("number");
describe = coffeeMap.get("describe");
cf_number.setText("数量:"+number);
cf_price.setText("价格:"+price);
cf_name.setText(""+name);
cf_describe.setText(""+describe);
iv_coffee.setImageResource(a);
}
}
package xdglyt.software.jhwz.myapplication11;
public class Coffee {
private String coffeeName;
private String coffeeNumber;
private String coffeePrice;
private String coffeeDescribe;
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCoffeeName() {
return coffeeName;
}
public void setCoffeeName(String coffeeName) {
this.coffeeName = coffeeName;
}
public String getCoffeeNumber() {
return coffeeNumber;
}
public void setCoffeeNumber(String coffeeNumber) {
this.coffeeNumber = coffeeNumber;
}
public String getCoffeePrice() {
return coffeePrice;
}
public void setCoffeePrice(String coffeePrice) {
this.coffeePrice = coffeePrice;
}
public String getCoffeeDescribe() {
return coffeeDescribe;
}
public void setCoffeeDescribe(String coffeeDescribe) {
this.coffeeDescribe = coffeeDescribe;
}
}
package xdglyt.software.jhwz.myapplication11;
import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class CoffeeService {
public static List<Coffee> getInfosFromXML(InputStream is) throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is,"utf-8");
List<Coffee> coffeeInfos = null;
Coffee coffeeInfo = null;
int type = parser.getEventType();
while (type!=XmlPullParser.END_DOCUMENT){
switch (type){
case XmlPullParser.START_TAG:
if("infos".equals(parser.getName())){
coffeeInfos = new ArrayList<Coffee>();
}else if("coffee".equals(parser.getName())){
coffeeInfo = new Coffee();
String id = parser.getAttributeName(0);
coffeeInfo.setId(id);
}else if("number".equals(parser.getName())){
String number = parser.nextText();
coffeeInfo.setCoffeeNumber(number);
}else if("price".equals(parser.getName())){
String price = parser.nextText();
coffeeInfo.setCoffeePrice(price);
}else if("name".equals(parser.getName())){
String name = parser.nextText();
coffeeInfo.setCoffeeName(name);
}else if("describe".equals(parser.getName())){
String describe = parser.nextText();
coffeeInfo.setCoffeeDescribe(describe);
}
break;
case XmlPullParser.END_TAG:
if ("coffee".equals(parser.getName())){
coffeeInfos.add(coffeeInfo);
coffeeInfo=null;
}
break;
}
type = parser.next();
}
return coffeeInfos;
}
}
<?xml version="1.0" encoding="utf-8"?>
<infos>
<coffee id="kbqn">
<number>20</number>
<price>26</price>
<name>卡布奇诺</name>
<describe>卡布奇诺是一种很好喝的咖啡!</describe>
</coffee>
<coffee id="mskf">
<number>56</number>
<price>20</price>
<name>美式咖啡</name>
<describe>美式咖啡是一种很好喝的咖啡!</describe>
</coffee>
<coffee id="nt">
<number>16</number>
<price>35</price>
<name>拿铁</name>
<describe>拿铁是一种很好喝的咖啡!</describe>
</coffee>
</infos>