自定义加减器

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">

    <Button
        android:layout_width="35dp"
        android:layout_height="30dp"
        android:id="@+id/button_add"
        android:textSize="10dp"
        android:gravity="center"
        android:text="+" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:text="1"
        android:gravity="center"
        android:id="@+id/text_number"
        />

    <Button
        android:layout_width="35dp"
        android:layout_height="30dp"
        android:textSize="10dp"
        android:gravity="center"
        android:id="@+id/button_reduce"
        android:text="-"
        />


</LinearLayout>

 

public class AddReduce extends LinearLayout {
    private Button button_add;
    private Button button_reduce;
    private EditText text_number;

    public AddReduce(Context context) {
        super(context);
    }

    public AddReduce(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initview(context);
    }

    //加
    private Add_Nums add_nums;

    public interface Add_Nums{
        void add(int add);
    }

    public void setAdd_nums(Add_Nums add_nums) {
        this.add_nums = add_nums;
    }

    //减
    private Jian_Num jian_num;

    public interface Jian_Num{
        void jian(int add);
    }

    public void setJian_num(Jian_Num jian_num) {
        this.jian_num = jian_num;
    }


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


    private void initview(final Context context) {

        View view = View.inflate(context, R.layout.add_reduce_layout,this);
        button_add = view.findViewById(R.id.button_add);
        button_reduce = view.findViewById(R.id.button_reduce);
        text_number = view.findViewById(R.id.text_number);
        //加
        button_add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String addnumber = text_number.getText().toString();
                Integer addnumberInt = new Integer(addnumber);
                addnumberInt ++;
                text_number.setText(addnumberInt+"");
                add_nums.add(new Integer(text_number.getText().toString()));
            }
        });
        //减
        button_reduce.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                String jiannumber = text_number.getText().toString();
                Integer jiannumberint = new Integer(jiannumber);
                if(jiannumberint>1){
                    jiannumberint--;
                    text_number.setText(jiannumberint+"");
                    jian_num.jian(new Integer(text_number.getText().toString()));
                }else{

                    Toast.makeText(context,"商品的数量不能小于1",Toast.LENGTH_SHORT).show();

                }
            }
        });
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值