带有增加与减少按钮的数量选择控件QuantityView

带有增加与减少按钮的数量选择控件



用法:
<me.himanshusoni.quantityview.QuantityView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/quantityView_default"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    app:qv_quantity="10" />

自定义
属性:
Xml代码   收藏代码
  1. app:qv_addButtonBackground="color|drawable"  
  2. app:qv_addButtonText="string"  
  3. app:qv_addButtonTextColor="color"  
  4. app:qv_removeButtonBackground="color|drawable"  
  5. app:qv_removeButtonText="string"  
  6. app:qv_removeButtonTextColor="color"  
  7. app:qv_quantityBackground="color|drawable"  
  8. app:qv_quantityTextColor="color"  
  9. app:qv_quantity="integer"  
  10. app:qv_quantityPadding="dimension"  
  11. app:qv_maxQuantity="integer"  
  12. app:qv_minQuantity="integer"  



Java代码   收藏代码
  1. import android.annotation.TargetApi;  
  2. import android.content.Context;  
  3. import android.content.DialogInterface;  
  4. import android.content.res.TypedArray;  
  5. import android.graphics.Color;  
  6. import android.graphics.drawable.Drawable;  
  7. import android.os.Build;  
  8. import android.support.v4.content.ContextCompat;  
  9. import android.support.v7.app.AlertDialog;  
  10. import android.util.AttributeSet;  
  11. import android.view.Gravity;  
  12. import android.view.LayoutInflater;  
  13. import android.view.View;  
  14. import android.widget.Button;  
  15. import android.widget.EditText;  
  16. import android.widget.LinearLayout;  
  17. import android.widget.TextView;  
  18.   
  19. /** 
  20.  * Quantity view to add and remove quantities 
  21.  */  
  22. public class QuantityView extends LinearLayout implements View.OnClickListener {  
  23.   
  24.     private Drawable quantityBackground, addButtonBackground, removeButtonBackground;  
  25.   
  26.     private String addButtonText, removeButtonText;  
  27.   
  28.     private int quantity, maxQuantity, minQuantity;  
  29.     private int quantityPadding;  
  30.   
  31.     private int quantityTextColor, addButtonTextColor, removeButtonTextColor;  
  32.   
  33.     private Button mButtonAdd, mButtonRemove;  
  34.     private TextView mTextViewQuantity;  
  35.   
  36.     public interface OnQuantityChangeListener {  
  37.         void onQuantityChanged(int newQuantity, boolean programmatically);  
  38.   
  39.         void onLimitReached();  
  40.     }  
  41.   
  42.     private OnQuantityChangeListener onQuantityChangeListener;  
  43.   
  44.     public QuantityView(Context context) {  
  45.         super(context);  
  46.         init(null0);  
  47.     }  
  48.   
  49.     public QuantityView(Context context, AttributeSet attrs) {  
  50.         super(context, attrs);  
  51.         init(attrs, 0);  
  52.     }  
  53.   
  54.     public QuantityView(Context context, AttributeSet attrs, int defStyle) {  
  55.         super(context, attrs, defStyle);  
  56.         init(attrs, defStyle);  
  57.     }  
  58.   
  59.     @TargetApi(Build.VERSION_CODES.JELLY_BEAN)  
  60.     private void init(AttributeSet attrs, int defStyle) {  
  61.         final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.QuantityView, defStyle, 0);  
  62.   
  63.         addButtonText = getResources().getString(R.string.qv_add);  
  64.         if (a.hasValue(R.styleable.QuantityView_qv_addButtonText)) {  
  65.             addButtonText = a.getString(R.styleable.QuantityView_qv_addButtonText);  
  66.         }  
  67.         addButtonBackground = ContextCompat.getDrawable(getContext(), R.drawable.qv_btn_selector);  
  68.         if (a.hasValue(R.styleable.QuantityView_qv_addButtonBackground)) {  
  69.             addButtonBackground = a.getDrawable(R.styleable.QuantityView_qv_addButtonBackground);  
  70.         }  
  71.         addButtonTextColor = a.getColor(R.styleable.QuantityView_qv_addButtonTextColor, Color.BLACK);  
  72.   
  73.         removeButtonText = getResources().getString(R.string.qv_remove);  
  74.         if (a.hasValue(R.styleable.QuantityView_qv_removeButtonText)) {  
  75.             removeButtonText = a.getString(R.styleable.QuantityView_qv_removeButtonText);  
  76.         }  
  77.         removeButtonBackground = ContextCompat.getDrawable(getContext(), R.drawable.qv_btn_selector);  
  78.         if (a.hasValue(R.styleable.QuantityView_qv_removeButtonBackground)) {  
  79.             removeButtonBackground = a.getDrawable(R.styleable.QuantityView_qv_removeButtonBackground);  
  80.         }  
  81.         removeButtonTextColor = a.getColor(R.styleable.QuantityView_qv_removeButtonTextColor, Color.BLACK);  
  82.   
  83.         quantity = a.getInt(R.styleable.QuantityView_qv_quantity, 0);  
  84.         maxQuantity = a.getInt(R.styleable.QuantityView_qv_maxQuantity, Integer.MAX_VALUE);  
  85.         minQuantity = a.getInt(R.styleable.QuantityView_qv_minQuantity, 0);  
  86.   
  87.         quantityPadding = (int) a.getDimension(R.styleable.QuantityView_qv_quantityPadding, pxFromDp(24));  
  88.         quantityTextColor = a.getColor(R.styleable.QuantityView_qv_quantityTextColor, Color.BLACK);  
  89.         quantityBackground = ContextCompat.getDrawable(getContext(), R.drawable.qv_bg_selector);  
  90.         if (a.hasValue(R.styleable.QuantityView_qv_quantityBackground)) {  
  91.             quantityBackground = a.getDrawable(R.styleable.QuantityView_qv_quantityBackground);  
  92.         }  
  93.   
  94.         a.recycle();  
  95.         int dp10 = pxFromDp(10);  
  96.   
  97.         mButtonAdd = new Button(getContext());  
  98.         mButtonAdd.setGravity(Gravity.CENTER);  
  99.         mButtonAdd.setPadding(dp10, dp10, dp10, dp10);  
  100.         mButtonAdd.setMinimumHeight(0);  
  101.         mButtonAdd.setMinimumWidth(0);  
  102.         mButtonAdd.setMinHeight(0);  
  103.         mButtonAdd.setMinWidth(0);  
  104.         setAddButtonBackground(addButtonBackground);  
  105.         setAddButtonText(addButtonText);  
  106.         setAddButtonTextColor(addButtonTextColor);  
  107.   
  108.         mButtonRemove = new Button(getContext());  
  109.         mButtonRemove.setGravity(Gravity.CENTER);  
  110.         mButtonRemove.setPadding(dp10, dp10, dp10, dp10);  
  111.         mButtonRemove.setMinimumHeight(0);  
  112.         mButtonRemove.setMinimumWidth(0);  
  113.         mButtonRemove.setMinHeight(0);  
  114.         mButtonRemove.setMinWidth(0);  
  115.         setRemoveButtonBackground(removeButtonBackground);  
  116.         setRemoveButtonText(removeButtonText);  
  117.         setRemoveButtonTextColor(removeButtonTextColor);  
  118.   
  119.         mTextViewQuantity = new TextView(getContext());  
  120.         mTextViewQuantity.setGravity(Gravity.CENTER);  
  121.         setQuantityTextColor(quantityTextColor);  
  122.         setQuantity(quantity);  
  123.         setQuantityBackground(quantityBackground);  
  124.         setQuantityPadding(quantityPadding);  
  125.   
  126.         setOrientation(HORIZONTAL);  
  127.   
  128.         addView(mButtonRemove, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  129.         addView(mTextViewQuantity, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);  
  130.         addView(mButtonAdd, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  131.   
  132.         mButtonAdd.setOnClickListener(this);  
  133.         mButtonRemove.setOnClickListener(this);  
  134.         mTextViewQuantity.setOnClickListener(this);  
  135.     }  
  136.   
  137.     @Override  
  138.     public void onClick(View v) {  
  139.         if (v == mButtonAdd) {  
  140.             if (quantity + 1 > maxQuantity) {  
  141.                 if (onQuantityChangeListener != null) onQuantityChangeListener.onLimitReached();  
  142.             } else {  
  143.                 quantity += 1;  
  144.                 mTextViewQuantity.setText(String.valueOf(quantity));  
  145.                 if (onQuantityChangeListener != null)  
  146.                     onQuantityChangeListener.onQuantityChanged(quantity, false);  
  147.             }  
  148.         } else if (v == mButtonRemove) {  
  149.             if (quantity - 1 < minQuantity) {  
  150.                 if (onQuantityChangeListener != null) onQuantityChangeListener.onLimitReached();  
  151.             } else {  
  152.                 quantity -= 1;  
  153.                 mTextViewQuantity.setText(String.valueOf(quantity));  
  154.                 if (onQuantityChangeListener != null)  
  155.                     onQuantityChangeListener.onQuantityChanged(quantity, false);  
  156.             }  
  157.         } else if (v == mTextViewQuantity) {  
  158.             AlertDialog.Builder builder = new AlertDialog.Builder(getContext());  
  159.             builder.setTitle("Change Quantity");  
  160.   
  161.             View inflate = LayoutInflater.from(getContext()).inflate(R.layout.qv_dialog_changequantity, nullfalse);  
  162.             final EditText et = (EditText) inflate.findViewById(R.id.qv_et_change_qty);  
  163.             et.setText(String.valueOf(quantity));  
  164.   
  165.             builder.setView(inflate);  
  166.             builder.setPositiveButton("Change"new DialogInterface.OnClickListener() {  
  167.                 @Override  
  168.                 public void onClick(DialogInterface dialog, int which) {  
  169.                     String newQuantity = et.getText().toString();  
  170.                     if (isNumber(newQuantity)) {  
  171.                         int intNewQuantity = Integer.parseInt(newQuantity);  
  172.                         setQuantity(intNewQuantity);  
  173.                     }  
  174.                 }  
  175.             }).setNegativeButton("Cancel"null);  
  176.             builder.show();  
  177.         }  
  178.     }  
  179.   
  180.   
  181.     public OnQuantityChangeListener getOnQuantityChangeListener() {  
  182.         return onQuantityChangeListener;  
  183.     }  
  184.   
  185.     public void setOnQuantityChangeListener(OnQuantityChangeListener onQuantityChangeListener) {  
  186.         this.onQuantityChangeListener = onQuantityChangeListener;  
  187.     }  
  188.   
  189.     public Drawable getQuantityBackground() {  
  190.         return quantityBackground;  
  191.     }  
  192.   
  193.     public void setQuantityBackground(Drawable quantityBackground) {  
  194.         this.quantityBackground = quantityBackground;  
  195.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {  
  196.             mTextViewQuantity.setBackground(quantityBackground);  
  197.         } else {  
  198.             mTextViewQuantity.setBackgroundDrawable(quantityBackground);  
  199.         }  
  200.     }  
  201.   
  202.     public Drawable getAddButtonBackground() {  
  203.         return addButtonBackground;  
  204.     }  
  205.   
  206.     public void setAddButtonBackground(Drawable addButtonBackground) {  
  207.         this.addButtonBackground = addButtonBackground;  
  208.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {  
  209.             mButtonAdd.setBackground(addButtonBackground);  
  210.         } else {  
  211.             mButtonAdd.setBackgroundDrawable(addButtonBackground);  
  212.         }  
  213.     }  
  214.   
  215.     public Drawable getRemoveButtonBackground() {  
  216.         return removeButtonBackground;  
  217.     }  
  218.   
  219.     public void setRemoveButtonBackground(Drawable removeButtonBackground) {  
  220.         this.removeButtonBackground = removeButtonBackground;  
  221.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {  
  222.             mButtonRemove.setBackground(removeButtonBackground);  
  223.         } else {  
  224.             mButtonRemove.setBackgroundDrawable(removeButtonBackground);  
  225.         }  
  226.     }  
  227.   
  228.     public String getAddButtonText() {  
  229.         return addButtonText;  
  230.     }  
  231.   
  232.     public void setAddButtonText(String addButtonText) {  
  233.         this.addButtonText = addButtonText;  
  234.         mButtonAdd.setText(addButtonText);  
  235.     }  
  236.   
  237.     public String getRemoveButtonText() {  
  238.         return removeButtonText;  
  239.     }  
  240.   
  241.     public void setRemoveButtonText(String removeButtonText) {  
  242.         this.removeButtonText = removeButtonText;  
  243.         mButtonRemove.setText(removeButtonText);  
  244.     }  
  245.   
  246.     public int getQuantity() {  
  247.         return quantity;  
  248.     }  
  249.   
  250.     public void setQuantity(int quantity) {  
  251.         boolean limitReached = false;  
  252.   
  253.         if (quantity > maxQuantity) {  
  254.             quantity = maxQuantity;  
  255.             limitReached = true;  
  256.             if (onQuantityChangeListener != null) onQuantityChangeListener.onLimitReached();  
  257.         }  
  258.         if (quantity < minQuantity) {  
  259.             quantity = minQuantity;  
  260.             limitReached = true;  
  261.             if (onQuantityChangeListener != null) onQuantityChangeListener.onLimitReached();  
  262.         }  
  263.   
  264.   
  265.         if (!limitReached && onQuantityChangeListener != null)  
  266.             onQuantityChangeListener.onQuantityChanged(quantity, true);  
  267.   
  268.         this.quantity = quantity;  
  269.         mTextViewQuantity.setText(String.valueOf(this.quantity));  
  270.     }  
  271.   
  272.     public int getMaxQuantity() {  
  273.         return maxQuantity;  
  274.     }  
  275.   
  276.     public void setMaxQuantity(int maxQuantity) {  
  277.         this.maxQuantity = maxQuantity;  
  278.     }  
  279.   
  280.     public int getMinQuantity() {  
  281.         return minQuantity;  
  282.     }  
  283.   
  284.     public void setMinQuantity(int minQuantity) {  
  285.         this.minQuantity = minQuantity;  
  286.     }  
  287.   
  288.     public int getQuantityPadding() {  
  289.         return quantityPadding;  
  290.     }  
  291.   
  292.     public void setQuantityPadding(int quantityPadding) {  
  293.         this.quantityPadding = quantityPadding;  
  294.         mTextViewQuantity.setPadding(quantityPadding, 0, quantityPadding, 0);  
  295.     }  
  296.   
  297.     public int getQuantityTextColor() {  
  298.         return quantityTextColor;  
  299.     }  
  300.   
  301.     public void setQuantityTextColor(int quantityTextColor) {  
  302.         this.quantityTextColor = quantityTextColor;  
  303.         mTextViewQuantity.setTextColor(quantityTextColor);  
  304.     }  
  305.   
  306.     public void setQuantityTextColorRes(int quantityTextColorRes) {  
  307.         this.quantityTextColor = ContextCompat.getColor(getContext(), quantityTextColorRes);  
  308.         mTextViewQuantity.setTextColor(quantityTextColor);  
  309.     }  
  310.   
  311.     public int getAddButtonTextColor() {  
  312.         return addButtonTextColor;  
  313.     }  
  314.   
  315.     public void setAddButtonTextColor(int addButtonTextColor) {  
  316.         this.addButtonTextColor = addButtonTextColor;  
  317.         mButtonAdd.setTextColor(addButtonTextColor);  
  318.     }  
  319.   
  320.     public void setAddButtonTextColorRes(int addButtonTextColorRes) {  
  321.         this.addButtonTextColor = ContextCompat.getColor(getContext(), addButtonTextColorRes);  
  322.         mButtonAdd.setTextColor(addButtonTextColor);  
  323.     }  
  324.   
  325.     public int getRemoveButtonTextColor() {  
  326.         return removeButtonTextColor;  
  327.     }  
  328.   
  329.     public void setRemoveButtonTextColor(int removeButtonTextColor) {  
  330.         this.removeButtonTextColor = removeButtonTextColor;  
  331.         mButtonRemove.setTextColor(removeButtonTextColor);  
  332.     }  
  333.   
  334.     public void setRemoveButtonTextColorRes(int removeButtonTextColorRes) {  
  335.         this.removeButtonTextColor = ContextCompat.getColor(getContext(), removeButtonTextColorRes);  
  336.         mButtonRemove.setTextColor(removeButtonTextColor);  
  337.     }  
  338.   
  339.     private int dpFromPx(final float px) {  
  340.         return (int) (px / getResources().getDisplayMetrics().density);  
  341.     }  
  342.   
  343.     private int pxFromDp(final float dp) {  
  344.         return (int) (dp * getResources().getDisplayMetrics().density);  
  345.     }  
  346.   
  347.   
  348.     private boolean isNumber(String string) {  
  349.         try {  
  350.             Integer.parseInt(string);  
  351.             return true;  
  352.         } catch (Exception e) {  
  353.             return false;  
  354.         }  
  355.     }  
  356.   
  357.   
  358. }  


qv_dialog_changequantity.xml
Xml代码   收藏代码
  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.     android:orientation="vertical">  
  6.   
  7.     <EditText  
  8.         android:id="@+id/qv_et_change_qty"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_margin="10dp"  
  12.         android:inputType="number" />  
  13.   
  14. </LinearLayout>  


attr.xml
Xml代码   收藏代码
  1. <resources>  
  2.     <declare-styleable name="QuantityView">  
  3.         <attr name="qv_addButtonText" format="string" />  
  4.         <attr name="qv_addButtonBackground" format="color|reference" />  
  5.         <attr name="qv_addButtonTextColor" format="color" />  
  6.   
  7.         <attr name="qv_removeButtonText" format="string" />  
  8.         <attr name="qv_removeButtonBackground" format="color|reference" />  
  9.         <attr name="qv_removeButtonTextColor" format="color" />  
  10.   
  11.         <attr name="qv_quantity" format="integer" />  
  12.         <attr name="qv_quantityBackground" format="color|reference" />  
  13.         <attr name="qv_quantityTextColor" format="color" />  
  14.         <attr name="qv_quantityPadding" format="dimension" />  
  15.   
  16.         <attr name="qv_maxQuantity" format="integer" />  
  17.         <attr name="qv_minQuantity" format="integer" />  
  18.     </declare-styleable>  
  19. </resources>  


string.xml
Xml代码   收藏代码
  1. <string name="qv_add">+</string>  
  2. <string name="qv_remove">-</string>  


drawable:
qv_bg_selector.xml
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_pressed="true">  
  4.         <shape android:shape="rectangle">  
  5.             <solid android:color="#ddd" />  
  6.         </shape>  
  7.     </item>  
  8.     <item android:state_selected="true">  
  9.         <shape android:shape="rectangle">  
  10.             <solid android:color="#ddd" />  
  11.         </shape>  
  12.     </item>  
  13.     <item>  
  14.         <shape android:shape="rectangle">  
  15.             <solid android:color="#eee" />  
  16.         </shape>  
  17.     </item>  
  18.   
  19. </selector>  


qv_btn_selector.xml
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_pressed="true">  
  4.         <shape android:shape="rectangle">  
  5.             <solid android:color="#ccc" />  
  6.         </shape>  
  7.     </item>  
  8.     <item android:state_selected="true">  
  9.         <shape android:shape="rectangle">  
  10.             <solid android:color="#ccc" />  
  11.         </shape>  
  12.     </item>  
  13.     <item>  
  14.         <shape android:shape="rectangle">  
  15.             <solid android:color="#ddd" />  
  16.         </shape>  
  17.     </item>  
  18.   
  19. </selector>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值