android实现简单的购物车加减器

这篇博客介绍了如何在Android应用中实现购物车商品数量的加减功能,通过EditText控件和自定义布局来完成这一操作,详细讲解了MainActivity.java和activity_main.xml中的关键代码实现。
摘要由CSDN通过智能技术生成

首先传两张需要用的图片

     add

    del



自定义

AddDecreaseView类

/**
 * Created by 撩个小媳妇 on 2018/5/10.
 */

public class AddDecreaseView extends LinearLayout{


    public AddDecreaseView(Context context) {
        this(context, null);
    }

    public AddDecreaseView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AddDecreaseView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private EditText etNum;

    //定义接口
    public interface OnAddDecreseClickListener {
        void onClickDecrease(View v);

        void onClickAdd(View v);
    }
    // 2. 声明一个接口对象
    private OnAddDecreseClickListener listener;
    // 3. 给外部提供一个设置接口对象的方法
    public void setOnAddDecreaseClickListener(OnAddDecreseClickListener listener) {
        this.listener = listener;
    }


    public int getNum() {
        return Integer.valueOf(etNum.getText().toString().trim());
    }

    public void setNum(int num) {
        etNum.setText(num + "");
    }

    private void initView(Context context) {
        // 注意:第三个参数,一定是this,代表添加视图到本View上
        View view = View.inflate(context, R.layout.view_add_decrease, this);

        TextView txtDecrease = view.findViewById(R.id.btn_del);
        TextView txtAdd = view.findViewById(R.id.btn_add);
        etNum = view.findViewById(R.id.et_num);

        // 6. 在合适的时机触发事件
        txtDecrease.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onClickDecrease(view);
                }
            }
        });

        txtAdd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onClickAdd(view);
                }
            }
        });
    }
}

        

MainActivity.java


public class MainActivity extends AppCompatActivity {

    private AddDecreaseView adv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adv = findViewById(R.id.adv_show);

        // 4. 实例化一个接口的对象
        AddDecreaseView.OnAddDecreseClickListener listener = new AddDecreaseView.OnAddDecreseClickListener() {
            @Override
            public void onClickDecrease(View v) {
                int num = adv.getNum();
                if (num > 1) {
                    num--;
                    adv.setNum(num);
                }
            }

            @Override
            public void onClickAdd(View v) {
                int num = adv.getNum();
                num++;
                adv.setNum(num);
            }
        };

        // 5.实例化好的对象传给内部
        adv.setOnAddDecreaseClickListener(listener);
    }
}


view_add_decrease.xml

这里我用的是EditText


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
    <Button
        android:id="@+id/btn_add"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/add"
        />
    <EditText
        android:id="@+id/et_num"
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:layout_weight="8"
        />
    <Button
        android:id="@+id/btn_del"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/del"
        />
</LinearLayout>

activity_main.xml

*注意换成自己的包名


<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="horizontal"
    tools:context="com.example.dianshang_day03.MainActivity">
    <com.example.dianshang_day03.AddDecreaseView
        android:id="@+id/adv_show"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></com.example.dianshang_day03.AddDecreaseView>
</LinearLayout>



实现简单购物车加减商品结算,可以按照以下步骤进行: 1. 创建一个商品类,包含商品名称、商品价格、商品数量等属性。 ``` public class Goods { private String name; private double price; private int quantity; public Goods(String name, double price, int quantity) { this.name = name; this.price = price; this.quantity = quantity; } // getter和setter方法 } ``` 2. 在Activity创建一个列表,用于展示购物车商品。 ``` List<Goods> goodsList = new ArrayList<>(); ``` 3. 创建一个适配器,将商品列表展示到RecyclerView。 ``` public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.ViewHolder> { private List<Goods> mGoodsList; static class ViewHolder extends RecyclerView.ViewHolder { TextView goodsName; TextView goodsPrice; TextView goodsQuantity; public ViewHolder(View view) { super(view); goodsName = view.findViewById(R.id.goods_name); goodsPrice = view.findViewById(R.id.goods_price); goodsQuantity = view.findViewById(R.id.goods_quantity); } } public GoodsAdapter(List<Goods> goodsList) { mGoodsList = goodsList; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goods_item, parent, false); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { Goods goods = mGoodsList.get(position); holder.goodsName.setText(goods.getName()); holder.goodsPrice.setText(String.valueOf(goods.getPrice())); holder.goodsQuantity.setText(String.valueOf(goods.getQuantity())); } @Override public int getItemCount() { return mGoodsList.size(); } } ``` 4. 在布局文件添加RecyclerView和结算按钮。 ``` <androidx.recyclerview.widget.RecyclerView android:id="@+id/goods_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" /> <Button android:id="@+id/checkout_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:text="结算" /> ``` 5. 在Activity初始化RecyclerView和适配器。 ``` RecyclerView recyclerView = findViewById(R.id.goods_list); LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); GoodsAdapter adapter = new GoodsAdapter(goodsList); recyclerView.setAdapter(adapter); ``` 6. 添加添加商品和删除商品的功能。 ``` // 添加商品 goodsList.add(new Goods("商品1", 10.0, 1)); adapter.notifyDataSetChanged(); // 删除商品 goodsList.remove(position); adapter.notifyItemRemoved(position); ``` 7. 添加加减商品数量的功能。 ``` // 商品数量加1 goods.setQuantity(goods.getQuantity() + 1); adapter.notifyItemChanged(position); // 商品数量减1 if (goods.getQuantity() > 0) { goods.setQuantity(goods.getQuantity() - 1); adapter.notifyItemChanged(position); } ``` 8. 添加结算功能。 ``` double totalPrice = 0; for (Goods goods : goodsList) { totalPrice += goods.getPrice() * goods.getQuantity(); } Toast.makeText(this, "总价为:" + totalPrice, Toast.LENGTH_SHORT).show(); ``` 这样,一个简单购物车加减商品结算就实现了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值