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>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值