自定义增加删除按钮

public class AddSubView extends LinearLayout implements View.OnClickListener {

   private ImageView btn_sub;
   private ImageView btn_add;
   private TextView tv_count;
   private int value = 0;
   private int minValue = 0;
   private int maxValue = 100;

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

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

   public AddSubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
       super(context, attrs, defStyleAttr);
       View view = View.inflate(context, R.layout.add_sub_layout, this);
       btn_sub = (ImageView) findViewById(R.id.iv_minus);
       btn_add = (ImageView) findViewById(R.id.iv_plus);
       tv_count = (TextView) findViewById(R.id.tv_number);
       getValue();
       // 设置点击事件
       btn_add.setOnClickListener(this);
       btn_sub.setOnClickListener(this);
       //设置状态
       setStatus();


   }

   private void setStatus() {
       //设置状态
       if (value == 0) {
           btn_sub.setVisibility(INVISIBLE);
           tv_count.setVisibility(INVISIBLE);
       } else {
           btn_sub.setVisibility(VISIBLE);
           tv_count.setVisibility(VISIBLE);
       }
   }

   public int getValue() {
       String countStr = tv_count.getText().toString().trim();//文本内容
       if (countStr != null) {
           value = Integer.valueOf(countStr);
       }
       return value;
   }

   public void setValue(int value) {
       this.value = value;
       setStatus();
       tv_count.setText(String.valueOf(value));
   }

   public int getMinValue() {
       return minValue;
   }

   public void setMinValue(int minValue) {
       this.minValue = minValue;
   }

   public int getMaxValue() {
       return maxValue;
   }

   public void setMaxValue(int maxValue) {
       this.maxValue = maxValue;
   }

   @Override
   public void onClick(View view) {
       if (view.getId() == R.id.iv_plus) {
           // 加
           addNumber();
           if (onNumberChangeListener != null) {
               onNumberChangeListener.addNumber(view, value);
           }
       } else {
           // 减
           subNumber();
           if (onNumberChangeListener != null) {
               onNumberChangeListener.subNumner(view, value);
           }
       }
   }

   private void subNumber() {
       if (value > minValue) {
           value -= 1;
       }
       setStatus();
       setValue(value);

   }

   private void addNumber() {
       if (value < maxValue) {
           value += 1;
       }
       setStatus();
       setValue(value);

   }

   public interface OnNumberChangeListener {
       // 当按钮被点击的时候回调
       void addNumber(View view, int value);

       void subNumner(View view, int value);
   }

   private OnNumberChangeListener onNumberChangeListener;

   public void setOnNumberChangeListener(OnNumberChangeListener
                                                 onNumberChangeListener) {
       this.onNumberChangeListener = onNumberChangeListener;

   }
}

···

你可能感兴趣的:(自定义增加删除按钮)