美文网首页
记录自己一个自定义的购物车加减控件

记录自己一个自定义的购物车加减控件

作者: 咖啡伴辣条 | 来源:发表于2018-11-08 15:46 被阅读0次

    自定义一个购物车加减控件,可以加减数量,可以编辑数量,可以设置数量最大值(库存数量)最小值(最少购买数量)

    购物车加减 .png 002.png
    
    public class ParityAddDelView extends LinearLayout {
    
        private OnAddDelClickListener listener;
        private TextView tvNumber;
        private int editNum = 0;
        private Button btnAdd, btnDel;
        private int minNum = 0;
        private int maxNum = 9999999;
    
    
        public void setOnAddDelClickListener(OnAddDelClickListener listener) {
            if (listener != null) {
                this.listener = listener;
            }
        }
    
    
        public interface OnAddDelClickListener {
            void onAddClick(int mNum);
    
    //        void onDelClick(View v);
        }
    
        public ParityAddDelView(Context context) {
            this(context, null);
        }
    
        public ParityAddDelView(Context context, @Nullable AttributeSet attrs) {
            this(context, attrs, 0);
    
        }
    
        public ParityAddDelView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            initView(context, attrs, defStyleAttr);
        }
    
        private void initView(final Context context, AttributeSet attrs, int defStyleAttr) {
            View.inflate(context, R.layout.layout_add_del_parity, this);
            btnAdd = findViewById(R.id.but_add);
            btnDel = findViewById(R.id.but_delete);
            tvNumber = findViewById(R.id.et_number);
    
            String middle_text = tvNumber.getText().toString().trim();
    
            if (TextUtils.isEmpty(middle_text)) {
                middle_text = "0";
            }
    
    
            editNum = Integer.parseInt(middle_text);
    
            btnAdd.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    editNum = getNumber();
                    if (editNum <= 0) {
                        editNum = 0;
                    }
                    if (maxNum == 0) {
                        editNum = 0;
                    } else if (editNum < minNum) {
                        editNum = minNum;
                    } else if (editNum >= maxNum) {
                        editNum = maxNum;
                    } else {
                        editNum++;
                    }
                    setClickNumber(editNum);
                    if (listener != null)
                        listener.onAddClick(editNum);
                }
            });
            btnDel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    editNum = getNumber();
                    if (editNum <= 0) {
                        editNum = 0;
                    } else if (editNum > maxNum) {
                        editNum = maxNum;
                    } else if (editNum <= minNum) {
                        editNum = 0;
                    } else {
                        editNum--;
                    }
                    setClickNumber(editNum);
                    if (listener != null)
                        listener.onAddClick(editNum);
    
                }
    
            });
    
            tvNumber.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    showEditNumDialog(context);
                }
            });
    
        }
    
        /**
         * 手动编辑购物车购买数量
         * @param context
         */
        private void showEditNumDialog(Context context) {
            final AddDelDialog dialog = new AddDelDialog(context);
            dialog.setNumber(getNumber(), maxNum, minNum);
            dialog.setOnConfirmclickListener(new AddDelDialog.OnConfirmclickListener() {
                @Override
                public void onConfirmClick(int number) {
                    setNumber(number);
                    if (listener != null) {
                        listener.onAddClick(getNumber());
                    }
                    dialog.dismiss();
                }
            });
            dialog.show();
        }
    
        private void setClickNumber(int number) {
            if (number > 0) {
                tvNumber.setText(number + "");
            } else {
                tvNumber.setText("0");
            }
        }
    
    
        /**
         * 对外提供设置EditText值的方法
         */
        public void setNumber(int number) {
            if (number > 0) {
                if (number > maxNum) {
                    tvNumber.setText(maxNum + "");
                } else if (number < minNum) {
                    tvNumber.setText("0");
                } else {
                    tvNumber.setText(number + "");
                }
            } else {
                tvNumber.setText("0");
            }
    
        }
    
        /**
         * 得到控件原来的值
         */
        public int getNumber() {
            int number = 0;
            try {
                String numberStr = tvNumber.getText().toString().trim();
                number = Integer.valueOf(numberStr);
            } catch (Exception e) {
                number = 0;
            }
            return number;
        }
    
        /**
         * 设置最小值
         *
         * @param number
         */
        public void setMinNum(int number) {
            if (number > 0) {
                minNum = number;
            } else {
                minNum = 0;
            }
        }
    
        /**
         * 设置最大值
         *
         * @param number
         */
        public void setMaxNum(int number) {
            if (number >= 0) {
                maxNum = number;
            } else {
                maxNum = 0;
            }
        }
    }
    

    布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        >
    
        <Button
            android:id="@+id/but_delete"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_gravity="center"
            android:background="@drawable/selector_shopcart_minus_btn"
            android:gravity="center"
            android:text="—"
            android:textColor="@color/black_33"
            android:textSize="12sp"
            />
    
    
        <TextView
            android:id="@+id/et_number"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_gravity="center"
            android:layout_toRightOf="@+id/but_delete"
            android:background="@android:color/white"
            android:gravity="center"
            android:text="0"
            android:inputType="number"
            android:minWidth="30dp"
            android:textColor="@color/black_66"
            android:textSize="14sp"
            />
    
        <View
            android:layout_width="wrap_content"
            android:layout_height="1dp"
            android:layout_toLeftOf="@+id/but_add"
            android:layout_toRightOf="@+id/but_delete"
            android:background="@color/gray_e6"
            />
    
        <View
            android:layout_width="wrap_content"
            android:layout_height="1dp"
            android:layout_alignBottom="@+id/but_delete"
            android:layout_toLeftOf="@+id/but_add"
            android:layout_toRightOf="@+id/but_delete"
            android:background="@color/gray_e6"
            />
    
    
        <Button
            android:id="@+id/but_add"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_gravity="center"
            android:layout_toRightOf="@+id/et_number"
            android:background="@drawable/selector_shopcart_add_btn"
            android:gravity="center"
            android:text="+"
            android:textColor="@color/black_33"
            android:textSize="12sp"
            />
    
    

    弹出的编辑框

    public class AddDelDialog extends Dialog  {
        private Button btConfim;
        private Button btWait;
        private Button btAdd;
        private Button btDel;
        private EditText etNum;
        private int editNumber;
        private int minNum = 0;
        private int maxNum = 9999999;
        private OnConfirmclickListener onConfirmclickListener;
    
    
        public AddDelDialog(@NonNull Context context) {
            super(context, R.style.SimpleDialog);
        }
    
        public AddDelDialog(@NonNull Context context, @StyleRes int themeResId) {
            super(context, themeResId);
        }
    
        protected AddDelDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
            super(context, cancelable, cancelListener);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dialog_edit_num);
            setCanceledOnTouchOutside(false);
            initView();
        }
    
        private void initView() {
            btConfim = findViewById(R.id.yes);
            btWait = findViewById(R.id.no);
            btAdd = findViewById(R.id.but_add);
            btDel = findViewById(R.id.but_delete);
            etNum = findViewById(R.id.et_number);
            etNum.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                    if(getNum()>maxNum){
                        setNumber(maxNum);
                    }
                }
            });
            btConfim.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onConfirmclickListener.onConfirmClick(getNum());
                }
            });
            btWait.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            btAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editNumber = getNum();
                    if (editNumber <= 0) {
                        editNumber = 0;
                    }
                    if (maxNum == 0) {
                        editNumber = 0;
                    } else if (editNumber < minNum) {
                        editNumber = minNum;
                    } else if (editNumber >= maxNum) {
                        editNumber = maxNum;
                    } else {
                        editNumber++;
                    }
                    setNumber(editNumber);
                }
            });
            btDel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editNumber = getNum();
                    if (editNumber <= 0) {
                        editNumber = 0;
                    } else {
                        editNumber--;
                    }
                    setNumber(editNumber);
                }
            });
            etNum.setText(editNumber+"");
            etNum.setSelection(etNum.length());
        }
    
        public int getNum(){
            int number = 0;
            try {
                String numberStr = etNum.getText().toString().trim();
                number = Integer.valueOf(numberStr);
            } catch (Exception e) {
                number = 0;
            }
            return number;
        }
    
    
        public interface OnConfirmclickListener {
             void onConfirmClick(int number);
        }
    
    
        public void setNumber(int number,int maxNum,int minNum){
            this.editNumber =number;
            this.maxNum = maxNum;
            this.minNum =minNum;
        }
    
        public void setOnConfirmclickListener(OnConfirmclickListener onConfirmclickListener){
            this.onConfirmclickListener = onConfirmclickListener;
        }
    
        public void setNumber(int number) {
            if (number >= 0) {
                if (number > maxNum) {
                    etNum.setText(maxNum + "");
                }
                etNum.setText(number + "");
            } else {
                etNum.setText("0");
            }
            etNum.setSelection(etNum.length());
        }
    
    }
    
    

    弹窗布局

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:background="@android:color/white">
    
        <TextView
            android:id="@+id/dialog_et_num_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:text="修改购买数量"
            android:textColor="@color/black_33"
            android:textSize="14sp"
            />
    
        <RelativeLayout
            android:id="@+id/rl_add_del"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_below="@+id/dialog_et_num_title"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            >
    
            <Button
                android:id="@+id/but_delete"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_gravity="center"
                android:background="@drawable/selector_shopcart_minus_btn"
                android:gravity="center"
                android:text="—"
                android:textColor="@color/black_33"
                android:textSize="12sp"
                />
    
    
            <EditText
                android:id="@+id/et_number"
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:layout_gravity="center"
                android:layout_toRightOf="@+id/but_delete"
                android:background="@android:color/white"
                android:gravity="center"
                android:inputType="number"
                android:lines="1"
                android:maxLength="10"
                android:minWidth="30dp"
                android:textColor="@color/black_66"
                android:textSize="10dp"
                android:text="0"
                />
    
            <View
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:layout_toLeftOf="@+id/but_add"
                android:layout_toRightOf="@+id/but_delete"
                android:background="@color/gray_e6"
                />
    
            <View
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:layout_alignBottom="@+id/but_delete"
                android:layout_toLeftOf="@+id/but_add"
                android:layout_toRightOf="@+id/but_delete"
                android:background="@color/gray_e6"
                />
    
    
            <Button
                android:id="@+id/but_add"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_gravity="center"
                android:layout_toRightOf="@+id/et_number"
                android:background="@drawable/selector_shopcart_add_btn"
                android:gravity="center"
                android:text="+"
                android:textColor="@color/black_33"
                android:textSize="12sp"
                />
        </RelativeLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_below="@+id/rl_add_del"
            android:layout_marginTop="20dp"
            android:background="@drawable/shape_simple_dialog"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/no"
                android:layout_width="0dp"
                android:layout_height="35dp"
                android:layout_weight="1"
                android:background="@color/black_99"
                android:gravity="center"
                android:singleLine="true"
                android:text="我再想想"
                android:textColor="@android:color/white"
                android:textSize="16sp"/>
    
            <View
                android:layout_width="1px"
                android:layout_height="match_parent"
                android:background="#E4E4E4"/>
    
            <Button
                android:id="@+id/yes"
                android:layout_width="0dp"
                android:layout_height="35dp"
                android:layout_weight="1"
                android:background="#e60012"
                android:gravity="center"
                android:singleLine="true"
                android:text="确定"
                android:textColor="@android:color/white"
                android:textSize="16sp"/>
        </LinearLayout>
    
    
    

    弹窗style

     <style name="SimpleDialog" parent="@android:style/Theme.Dialog">
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowIsTranslucent">true</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:backgroundDimEnabled">true</item>
            <item name="android:windowFrame">@null</item>
        </style>
    

    相关文章

      网友评论

          本文标题:记录自己一个自定义的购物车加减控件

          本文链接:https://www.haomeiwen.com/subject/ssswxqtx.html