Android代码有五个商品供选择购买,设计界面显示其品名及价格,用户可以多选,选择完毕后单击“结账”按钮,系统在屏幕下方显示总金额。
时间: 2024-02-26 19:56:23 浏览: 33
好的,以下是一个简单的 Android 实现,实现您所描述的购物系统:
1. 创建一个名为 `MainActivity` 的 Activity;
2. 在 `activity_main.xml` 布局文件中添加一个 `ListView` 和一个 `Button`;
3. 在 `MainActivity.java` 中编写代码实现商品列表的显示、选择和结账功能。
具体实现步骤如下:
1. 在 `activity_main.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/list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/checkout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="结账" />
</LinearLayout>
```
2. 在 `MainActivity.java` 中添加以下代码:
```java
public class MainActivity extends AppCompatActivity {
private List<Product> products = new ArrayList<>();
private List<Product> selectedProducts = new ArrayList<>();
private ListView listView;
private Button checkoutButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化商品列表
products.add(new Product("商品1", 10));
products.add(new Product("商品2", 20));
products.add(new Product("商品3", 30));
products.add(new Product("商品4", 40));
products.add(new Product("商品5", 50));
// 初始化 ListView
listView = findViewById(R.id.list_view);
ProductAdapter adapter = new ProductAdapter(this, R.layout.product_item, products);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Product product = products.get(position);
CheckBox checkBox = view.findViewById(R.id.checkbox);
if (checkBox.isChecked()) {
selectedProducts.remove(product);
} else {
selectedProducts.add(product);
}
checkBox.setChecked(!checkBox.isChecked());
}
});
// 初始化结账按钮
checkoutButton = findViewById(R.id.checkout_button);
checkoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int total = 0;
for (Product product : selectedProducts) {
total += product.getPrice();
}
Toast.makeText(MainActivity.this, "总金额为 $" + total, Toast.LENGTH_SHORT).show();
}
});
}
private static class ProductAdapter extends ArrayAdapter<Product> {
private int resourceId;
public ProductAdapter(Context context, int resourceId, List<Product> products) {
super(context, resourceId, products);
this.resourceId = resourceId;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Product product = getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
TextView nameTextView = view.findViewById(R.id.name_text_view);
TextView priceTextView = view.findViewById(R.id.price_text_view);
CheckBox checkBox = view.findViewById(R.id.checkbox);
nameTextView.setText(product.getName());
priceTextView.setText("$" + product.getPrice());
checkBox.setChecked(selectedProducts.contains(product));
return view;
}
}
private static class Product {
private String name;
private int price;
public Product(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
}
}
```
这段代码实现了一个 `Product` 类来表示商品,一个 `ProductAdapter` 类来实现商品列表的显示和选择,以及一个 `setOnClickListener()` 方法来实现结账功能。在 `onCreate()` 方法中,我们首先初始化商品列表和 ListView,然后设置 ListView 的适配器和点击事件监听器。最后,我们初始化结账按钮并设置点击事件监听器,在监听器中计算所选商品的总金额并使用 `Toast` 在屏幕下方显示出来。
注意:这里的 `ProductAdapter` 类和 `product_item.xml` 布局文件并没有在代码中给出,您可以根据需要进行实现。