Android组合式自定义控件实现购物车加减商品操作
程序员文章站
2022-05-27 10:26:15
本文实例为大家分享了android实现购物车加减商品操作的具体代码,供大家参考,具体内容如下
mainactivity.java
public class ma...
本文实例为大家分享了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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。