前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android组合式自定义控件实现购物车加减商品操作

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

作者头像
砸漏
发布2020-11-04 15:29:44
1.1K0
发布2020-11-04 15:29:44
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

本文实例为大家分享了Android实现购物车加减商品操作的具体代码,供大家参考,具体内容如下

MainActivity.java

代码语言:javascript
复制
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

代码语言:javascript
复制
<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

代码语言:javascript
复制
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

代码语言:javascript
复制
<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 

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档