自定义组合控件实现 购物车加减的简单实现

 简单实现购物车加减

     效果图




一、技术要求:

组合式自定义控件的实现

 

二、业务逻辑需求:

1.创建组合式自定义控件

2.实现图中合理布局

3.实现点击加号减号后商品数量改变的逻辑

4.商品数量最小不能少于1,最大不能多余5

5.使用静态代理设计模式把商品数量回调到Activity中并打印


首先在values文件下创建一个attrs样式

  attrs样式

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="AddDeleteViewStyle">  
  4.         <attr name="left_text" format="string"/>  
  5.         <attr name="right_text" format="string"/>  
  6.         <attr name="middle_text" format="string"/>  
  7.     </declare-styleable>  
  8. </resources>  


    布局

activity——main

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  5.     xmlns:tools="http://schemas.android.com/tools"  
  6.     android:layout_width="match_parent"  
  7.     android:layout_height="match_parent"  
  8.     android:padding="10dp"  
  9.      >  
  10.  <LinearLayout  
  11.      android:id="@+id/ll"  
  12.      android:layout_width="match_parent"  
  13.      android:layout_height="wrap_content"  
  14.      android:orientation="horizontal"  
  15.      android:weightSum="2"  
  16.      >  
  17.   <ImageView  
  18.       android:id="@+id/imageView"  
  19.       android:layout_width="120dp"  
  20.       android:layout_height="120dp"  
  21.       android:src="@drawable/gouwuche2"  
  22.       />  
  23.   <LinearLayout  
  24.       android:layout_width="match_parent"  
  25.       android:layout_height="wrap_content"  
  26.       android:orientation="vertical"  
  27.       android:weightSum="2"  
  28.       >  
  29.    <TextView  
  30.        android:id="@+id/tv_title"  
  31.        android:layout_width="match_parent"  
  32.        android:layout_height="wrap_content"  
  33.        android:src="@mipmap/ic_launcher"  
  34.        android:padding="5dp"  
  35.        android:layout_marginTop="20dp"  
  36.        android:text="
    不一样的美女,不一样的价格,亮瞎你的眼。
  37. "  
  38.        />  
  39.    <LinearLayout  
  40.        android:layout_width="match_parent"  
  41.        android:layout_height="wrap_content"  
  42.        android:orientation="horizontal"  
  43.        android:weightSum="2"  
  44.        >  
  45.     <TextView  
  46.         android:layout_width="0dp"  
  47.         android:layout_height="wrap_content"  
  48.         android:layout_weight="1"  
  49.         android:textColor="#f00"  
  50.         android:text="105000.00"  
  51.         />  
  52.     <TextView  
  53.         android:layout_width="0dp"  
  54.         android:layout_height="wrap_content"  
  55.         android:layout_weight="1"  
  56.         android:text="库存3000件"  
  57.         />  
  58.    </LinearLayout>  
  59.   </LinearLayout>  
  60.  </LinearLayout>  
  61.  <LinearLayout  
  62.      android:id="@+id/lll"  
  63.      android:layout_below="@+id/ll"  
  64.      android:layout_width="match_parent"  
  65.      android:layout_height="wrap_content"  
  66.      android:padding="10dp"  
  67.      android:weightSum="2"  
  68.      >  
  69.   <TextView  
  70.       android:layout_width="0dp"  
  71.       android:layout_weight="1"  
  72.       android:layout_height="wrap_content"  
  73.       android:text="购买数量"  
  74.       android:layout_gravity="center"  
  75.       />  
  76.   <!--自定义控件加减器-->  
  77.   <com. 此处换成自己的包名引入.JiaJianQi  
  78.       android:id="@+id/adv_main"  
  79.       android:layout_width="0dp"  
  80.       android:layout_weight="1"  
  81.       android:layout_height="wrap_content"  
  82.       app:left_text="-"  
  83.       app:middle_text="1"  
  84.       app:right_text="+">  
  85.   </com. 此处换成自己的包名引入.JiaJianQi>  
  86.  </LinearLayout>  
  87.  <Button  
  88.      android:layout_below="@id/lll"  
  89.      android:layout_width="wrap_content"  
  90.      android:layout_height="wrap_content"  
  91.      android:text="加入购物车"  
  92.      android:layout_marginTop="20dp"  
  93.      android:background="@android:color/holo_orange_light"  
  94.      />  
  95.  <Button  
  96.      android:layout_below="@id/lll"  
  97.      android:layout_width="wrap_content"  
  98.      android:layout_height="wrap_content"  
  99.      android:text="立即购买"  
  100.      android:layout_marginLeft="260dp"  
  101.      android:layout_marginTop="20dp"  
  102.      android:background="@android:color/holo_orange_light"  
  103.      />  
  104. </RelativeLayout>  

item_add_delete

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.     <LinearLayout  
  6.         android:layout_width="match_parent"  
  7.         android:layout_height="match_parent"  
  8.         android:orientation="horizontal"  
  9.         >  
  10.         <Button  
  11.             android:id="@+id/but_delete"  
  12.             android:layout_width="50dp"  
  13.             android:layout_height="50dp"  
  14.             android:text="-"  
  15.             android:textSize="20dp"  
  16.             android:background="@android:color/holo_orange_light"  
  17.             />  
  18.         <EditText  
  19.             android:id="@+id/et_number"  
  20.             android:layout_width="50dp"  
  21.             android:inputType="number"  
  22.             android:layout_height="wrap_content"  
  23.             android:gravity="center"  
  24.   
  25.             />  
  26.         <Button  
  27.             android:id="@+id/but_add"  
  28.             android:layout_width="50dp"  
  29.             android:layout_height="50dp"  
  30.             android:text="+"  
  31.             android:textSize="20dp"  
  32.             android:background="@android:color/holo_orange_light"  
  33.             />  
  34.     </LinearLayout>  
  35. </LinearLayout>  

MainActivity

[html]  view plain  copy
  1.    
  2. import android.os.Bundle;  
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.util.Log;  
  5. import android.view.View;  
  6. import android.widget.Toast;  
  7. public class MainActivity extends AppCompatActivity {  
  8.     private static final String TAG = "MainActivity";  
  9.     private JiaJianQi adv;  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.          setContentView(R.layout.activity_main);  
  14.         initView();  
  15.     }  
  16.     private void initView() {  
  17.         adv = (JiaJianQi) findViewById(R.id.adv_main);  
  18.         adv.OnJiaJianClickListener(new JiaJianQi.OnJiaJianClickListener() {  
  19.             @Override  
  20.             public void onAddClick(View v) {  
  21.                 Log.i(TAG, "onAddClick: 执行");  
  22.                 int origin = adv.getNumber();  
  23.                 if(origin<=4){  
  24.                 origin++;  
  25.                 Toast.makeText(MainActivity.this, "最多不能多于5个", Toast.LENGTH_SHORT).show();  
  26.                 adv.setNumber(origin);  
  27.                 }  
  28.             }  
  29.             @Override  
  30.             public void onDelClick(View v) {  
  31.                 int origin = adv.getNumber();  
  32.                 if(origin==1){  
  33.                 origin--;  
  34.                 Toast.makeText(MainActivity.this, "最少不能小于1个", Toast.LENGTH_SHORT).show();  
  35.                 adv.setNumber(origin);  
  36.                 }  
  37.             }  
  38.         });  
  39.     }  
  40. }  

JiaJianQi自定义类

[html]  view plain  copy
  1.    
  2. import android.content.Context;  
  3. import android.content.res.TypedArray;  
  4. import android.support.annotation.Nullable;  
  5. import android.util.AttributeSet;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. import android.widget.LinearLayout;  
  10. public class JiaJianQi extends LinearLayout{  
  11.    private OnJiaJianClickListener listener;  
  12.     private EditText ed_number;  
  13.     private String numberStr;  
  14.   
  15.     public void OnJiaJianClickListener(OnJiaJianClickListener listener) {  
  16.         if(listener!=null){  
  17.             this.listener = listener;  
  18.         }  
  19.     }  
  20.     public interface OnJiaJianClickListener{  
  21.         void onAddClick(View v);  
  22.         void onDelClick(View v);  
  23.     }  
  24.     public JiaJianQi(Context context) {  
  25.         this(context,null);  
  26.     }  
  27.   
  28.     public JiaJianQi(Context context, @Nullable AttributeSet attrs) {  
  29.         this(context, attrs,0);  
  30.     }  
  31.   
  32.     public JiaJianQi(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {  
  33.         super(context, attrs, defStyleAttr);  
  34.         initView(context, attrs, defStyleAttr);  
  35.     }  
  36.     private void initView(Context context, AttributeSet attrs, int defStyleAttr) {  
  37.         View.inflate(context, R.layout.item_add_delete,this);  
  38.         Button but_add=findViewById(R.id.but_add);  
  39.         Button but_delete=findViewById(R.id.but_delete);  
  40.         ed_number=findViewById(R.id.et_number);  
  41.           
  42.         //styleable.AddDeleteViewStyle它是attrs里的样式  
  43.         TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AddDeleteViewStyle);  
  44.         String left_text = typedArray.getString(R.styleable.AddDeleteViewStyle_left_text);  
  45.         String middle_text = typedArray.getString(R.styleable.AddDeleteViewStyle_middle_text);  
  46.         String right_text = typedArray.getString(R.styleable.AddDeleteViewStyle_right_text);  
  47.         but_delete.setText(left_text);  
  48.         but_add.setText(right_text);  
  49.         ed_number.setText(middle_text);  
  50.         //释放资源  
  51.         typedArray.recycle();  
  52.         but_add.setOnClickListener(new OnClickListener() {  
  53.             @Override  
  54.             public void onClick(View view) {  
  55.                 listener.onAddClick(view);  
  56.             }  
  57.         });  
  58.         but_delete.setOnClickListener(new OnClickListener() {  
  59.             @Override  
  60.             public void onClick(View view) {  
  61.                 listener.onDelClick(view);  
  62.             }  
  63.         });  
  64.     }  
  65.     /**  
  66.      * 对外提供设置EditText值的方法  
  67.      */  
  68.     public void setNumber(int number){  
  69.         if (number>0){  
  70.             ed_number.setText(number+"");  
  71.         }  
  72.     }  
  73.     /**  
  74.      * 得到控件原来的值  
  75.      */  
  76.     public int getNumber(){  
  77.         int number = 0;  
  78.         try {  
  79.             numberStr = ed_number.getText().toString().trim();  
  80.             number = Integer.valueOf(numberStr);  
  81.         } catch (Exception e) {  
  82.             number = 0;  
  83.         }  
  84.         return number;  
  85.     }  
  86. }  

猜你喜欢

转载自blog.csdn.net/zhang1223665986/article/details/80491300
今日推荐