SoFunction
Updated on 2025-04-06

Android combined custom controls implement shopping cart addition and subtraction operations

This article shares the specific code for Android to add or subtract goods operation for your reference. The specific content is as follows

public class MainActivity extends AppCompatActivity {

    private Addand mAddand;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      (savedInstanceState);
      setContentView(.activity_main);
      mAddand= findViewById();
  
      (new () {
        @Override
        public void OnNumberChanged(int vs) {
          (, vs+"", Toast.LENGTH_SHORT).show();
        }
      });
    }
  }

activity_main.xml

< xmlns:andro
  xmlns:app="/apk/res-auto"
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity"
  android:orientation="horizontal">

  <
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</>

public class Addand extends FrameLayout implements {
  private ImageView mImage1;
  private ImageView mImage2;
  private TextView mText;
  int value;

  public Addand(@NonNull Context context) {
    this(context,null);
  }

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

  public Addand(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    findView(context);
  }

  private void findView(Context context) {
    View view = (context, , this);

     mImage1 =(.image1);
     mImage2 = (.image2);
     mText = ();

     value=getValue();

     setValue(value);

     (this);
     (this);
  }
  private int vs=1;
  public int getValue() { //Get the value
    String trim = ().toString().trim();
    if (!(trim)){
      (vs);
    }
    return vs;
  }

  public void setValue(int value) {
    (value+"");
  }

  @Override
  public void onClick(View view) {

    switch (()){
      case .image1:
        add();
        break;
      case .image2:
        jian();
        break;
    }
  }

  private void jian() {
    if (vs&gt;1){
      vs--;
      setValue(vs);
    }

    (vs);
  }

  private void add() {

    if (vs&lt;6){
      vs++;
      setValue(vs);
    }

    (vs);
  }

  public interface OnNumberChangedListener{
    void OnNumberChanged(int vs);
  }

  private OnNumberChangedListener mOnNumberChangedListener;

  public void setOnNumberChangedListener(OnNumberChangedListener onNumberChangedListener){
      mOnNumberChangedListener=onNumberChangedListener;
  }

}

<LinearLayout xmlns:andro
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">

  <ImageView
    android:
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:src="@drawable/ic_launcher_background"/>

  <TextView
    android:
    android:layout_width="50dp"
    android:layout_height="20dp"
    android:gravity="center"
    android:text="1"/>

  <ImageView
    android:
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:src="@drawable/ic_launcher_background"/>
</LinearLayout>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.