Android中设计一个购物车页面可实现商品数量的增减、商品的删除以及总价格计算功能。给出代码,页面布局用线性布局
时间: 2024-02-15 11:03:46 浏览: 166
好的,请参考下面的代码实现:
布局文件 cart_layout.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--商品列表-->
<ListView
android:id="@+id/lv_cart"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<!--底部总价和结算按钮-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<!--总价-->
<TextView
android:id="@+id/tv_total_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"
android:text="总价:0元" />
<!--结算按钮-->
<Button
android:id="@+id/btn_checkout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结算" />
</LinearLayout>
</LinearLayout>
```
Java代码 CartActivity.java:
```java
public class CartActivity extends AppCompatActivity {
private ListView listView;
private CartAdapter adapter;
private List<CartItem> cartItems;
private TextView tvTotalPrice;
private Button btnCheckout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cart_layout);
listView = findViewById(R.id.lv_cart);
tvTotalPrice = findViewById(R.id.tv_total_price);
btnCheckout = findViewById(R.id.btn_checkout);
// 初始化购物车数据
cartItems = new ArrayList<>();
cartItems.add(new CartItem("商品1", 10, 1));
cartItems.add(new CartItem("商品2", 20, 2));
cartItems.add(new CartItem("商品3", 30, 3));
cartItems.add(new CartItem("商品4", 40, 4));
cartItems.add(new CartItem("商品5", 50, 5));
// 设置适配器
adapter = new CartAdapter(this, cartItems);
listView.setAdapter(adapter);
// 设置总价
tvTotalPrice.setText("总价:" + getTotalPrice() + "元");
// 设置结算按钮的点击事件
btnCheckout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 结算
Toast.makeText(CartActivity.this, "结算", Toast.LENGTH_SHORT).show();
}
});
}
// 获取总价
private int getTotalPrice() {
int totalPrice = 0;
for (CartItem item : cartItems) {
totalPrice += item.getPrice() * item.getCount();
}
return totalPrice;
}
// 购物车适配器
private class CartAdapter extends BaseAdapter {
private Context context;
private List<CartItem> cartItems;
public CartAdapter(Context context, List<CartItem> cartItems) {
this.context = context;
this.cartItems = cartItems;
}
@Override
public int getCount() {
return cartItems.size();
}
@Override
public Object getItem(int position) {
return cartItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.cart_item_layout, null);
holder = new ViewHolder();
holder.tvName = convertView.findViewById(R.id.tv_name);
holder.tvPrice = convertView.findViewById(R.id.tv_price);
holder.tvCount = convertView.findViewById(R.id.tv_count);
holder.btnIncrease = convertView.findViewById(R.id.btn_increase);
holder.btnDecrease = convertView.findViewById(R.id.btn_decrease);
holder.btnDelete = convertView.findViewById(R.id.btn_delete);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// 设置数据
final CartItem item = cartItems.get(position);
holder.tvName.setText(item.getName());
holder.tvPrice.setText("单价:" + item.getPrice() + "元");
holder.tvCount.setText(String.valueOf(item.getCount()));
// 增加商品数量
holder.btnIncrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
item.setCount(item.getCount() + 1);
notifyDataSetChanged();
tvTotalPrice.setText("总价:" + getTotalPrice() + "元");
}
});
// 减少商品数量
holder.btnDecrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (item.getCount() > 1) {
item.setCount(item.getCount() - 1);
notifyDataSetChanged();
tvTotalPrice.setText("总价:" + getTotalPrice() + "元");
}
}
});
// 删除商品
holder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cartItems.remove(position);
notifyDataSetChanged();
tvTotalPrice.setText("总价:" + getTotalPrice() + "元");
}
});
return convertView;
}
private class ViewHolder {
TextView tvName;
TextView tvPrice;
TextView tvCount;
Button btnIncrease;
Button btnDecrease;
Button btnDelete;
}
}
// 商品类
private class CartItem {
private String name; // 商品名称
private int price; // 商品单价
private int count; // 商品数量
public CartItem(String name, int price, int count) {
this.name = name;
this.price = price;
this.count = count;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
}
```
注:上述代码仅供参考,可能有一些细节需要根据实际情况进行调整。
阅读全文