温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android组合式自定义控件如何实现购物车加减商品操作

发布时间:2021-08-04 14:38:15 来源:亿速云 阅读:89 作者:小新 栏目:移动开发

这篇文章主要为大家展示了“Android组合式自定义控件如何实现购物车加减商品操作”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Android组合式自定义控件如何实现购物车加减商品操作”这篇文章吧。

具体内容如下

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Addand mAddand;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      mAddand= findViewById(R.id.add);
  
      mAddand.setOnNumberChangedListener(new Addand.OnNumberChangedListener() {
        @Override
        public void OnNumberChanged(int vs) {
          Toast.makeText(MainActivity.this, vs+"", Toast.LENGTH_SHORT).show();
        }
      });
    }
  }

activity_main.xml

<android.support.constraint.ConstraintLayout 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"
  tools:context=".MainActivity"
  android:orientation="horizontal">

  <fanruiqi.www.com.jia.Addand
    android:id="@+id/add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</android.support.constraint.ConstraintLayout>

Addand.java

public class Addand extends FrameLayout implements View.OnClickListener{
  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 = View.inflate(context, R.layout.add, this);

     mImage1 =view.findViewById(R.id.image1);
     mImage2 = view.findViewById(R.id.image2);
     mText = view.findViewById(R.id.text);

     value=getValue();

     setValue(value);

     mImage1.setOnClickListener(this);
     mImage2.setOnClickListener(this);
  }
  private int vs=1;
  public int getValue() { //获取值

    String trim = mText.getText().toString().trim();
    if (!TextUtils.isEmpty(trim)){
      Integer.valueOf(vs);
    }
    return vs;
  }

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

  @Override
  public void onClick(View view) {

    switch (view.getId()){
      case R.id.image1:
        add();
        break;
      case R.id.image2:
        jian();
        break;
    }
  }

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

    mOnNumberChangedListener.OnNumberChanged(vs);
  }

  private void add() {

    if (vs<6){
      vs++;
      setValue(vs);
    }

    mOnNumberChangedListener.OnNumberChanged(vs);
  }

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

  private OnNumberChangedListener mOnNumberChangedListener;

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

}

add.xml

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

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

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

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

以上是“Android组合式自定义控件如何实现购物车加减商品操作”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI