`

带有增加与减少按钮的数量选择控件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" />

自定义
属性:
app:qv_addButtonBackground="color|drawable"
app:qv_addButtonText="string"
app:qv_addButtonTextColor="color"
app:qv_removeButtonBackground="color|drawable"
app:qv_removeButtonText="string"
app:qv_removeButtonTextColor="color"
app:qv_quantityBackground="color|drawable"
app:qv_quantityTextColor="color"
app:qv_quantity="integer"
app:qv_quantityPadding="dimension"
app:qv_maxQuantity="integer"
app:qv_minQuantity="integer"



import android.annotation.TargetApi;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Quantity view to add and remove quantities
 */
public class QuantityView extends LinearLayout implements View.OnClickListener {

    private Drawable quantityBackground, addButtonBackground, removeButtonBackground;

    private String addButtonText, removeButtonText;

    private int quantity, maxQuantity, minQuantity;
    private int quantityPadding;

    private int quantityTextColor, addButtonTextColor, removeButtonTextColor;

    private Button mButtonAdd, mButtonRemove;
    private TextView mTextViewQuantity;

    public interface OnQuantityChangeListener {
        void onQuantityChanged(int newQuantity, boolean programmatically);

        void onLimitReached();
    }

    private OnQuantityChangeListener onQuantityChangeListener;

    public QuantityView(Context context) {
        super(context);
        init(null, 0);
    }

    public QuantityView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public QuantityView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void init(AttributeSet attrs, int defStyle) {
        final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.QuantityView, defStyle, 0);

        addButtonText = getResources().getString(R.string.qv_add);
        if (a.hasValue(R.styleable.QuantityView_qv_addButtonText)) {
            addButtonText = a.getString(R.styleable.QuantityView_qv_addButtonText);
        }
        addButtonBackground = ContextCompat.getDrawable(getContext(), R.drawable.qv_btn_selector);
        if (a.hasValue(R.styleable.QuantityView_qv_addButtonBackground)) {
            addButtonBackground = a.getDrawable(R.styleable.QuantityView_qv_addButtonBackground);
        }
        addButtonTextColor = a.getColor(R.styleable.QuantityView_qv_addButtonTextColor, Color.BLACK);

        removeButtonText = getResources().getString(R.string.qv_remove);
        if (a.hasValue(R.styleable.QuantityView_qv_removeButtonText)) {
            removeButtonText = a.getString(R.styleable.QuantityView_qv_removeButtonText);
        }
        removeButtonBackground = ContextCompat.getDrawable(getContext(), R.drawable.qv_btn_selector);
        if (a.hasValue(R.styleable.QuantityView_qv_removeButtonBackground)) {
            removeButtonBackground = a.getDrawable(R.styleable.QuantityView_qv_removeButtonBackground);
        }
        removeButtonTextColor = a.getColor(R.styleable.QuantityView_qv_removeButtonTextColor, Color.BLACK);

        quantity = a.getInt(R.styleable.QuantityView_qv_quantity, 0);
        maxQuantity = a.getInt(R.styleable.QuantityView_qv_maxQuantity, Integer.MAX_VALUE);
        minQuantity = a.getInt(R.styleable.QuantityView_qv_minQuantity, 0);

        quantityPadding = (int) a.getDimension(R.styleable.QuantityView_qv_quantityPadding, pxFromDp(24));
        quantityTextColor = a.getColor(R.styleable.QuantityView_qv_quantityTextColor, Color.BLACK);
        quantityBackground = ContextCompat.getDrawable(getContext(), R.drawable.qv_bg_selector);
        if (a.hasValue(R.styleable.QuantityView_qv_quantityBackground)) {
            quantityBackground = a.getDrawable(R.styleable.QuantityView_qv_quantityBackground);
        }

        a.recycle();
        int dp10 = pxFromDp(10);

        mButtonAdd = new Button(getContext());
        mButtonAdd.setGravity(Gravity.CENTER);
        mButtonAdd.setPadding(dp10, dp10, dp10, dp10);
        mButtonAdd.setMinimumHeight(0);
        mButtonAdd.setMinimumWidth(0);
        mButtonAdd.setMinHeight(0);
        mButtonAdd.setMinWidth(0);
        setAddButtonBackground(addButtonBackground);
        setAddButtonText(addButtonText);
        setAddButtonTextColor(addButtonTextColor);

        mButtonRemove = new Button(getContext());
        mButtonRemove.setGravity(Gravity.CENTER);
        mButtonRemove.setPadding(dp10, dp10, dp10, dp10);
        mButtonRemove.setMinimumHeight(0);
        mButtonRemove.setMinimumWidth(0);
        mButtonRemove.setMinHeight(0);
        mButtonRemove.setMinWidth(0);
        setRemoveButtonBackground(removeButtonBackground);
        setRemoveButtonText(removeButtonText);
        setRemoveButtonTextColor(removeButtonTextColor);

        mTextViewQuantity = new TextView(getContext());
        mTextViewQuantity.setGravity(Gravity.CENTER);
        setQuantityTextColor(quantityTextColor);
        setQuantity(quantity);
        setQuantityBackground(quantityBackground);
        setQuantityPadding(quantityPadding);

        setOrientation(HORIZONTAL);

        addView(mButtonRemove, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        addView(mTextViewQuantity, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        addView(mButtonAdd, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        mButtonAdd.setOnClickListener(this);
        mButtonRemove.setOnClickListener(this);
        mTextViewQuantity.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == mButtonAdd) {
            if (quantity + 1 > maxQuantity) {
                if (onQuantityChangeListener != null) onQuantityChangeListener.onLimitReached();
            } else {
                quantity += 1;
                mTextViewQuantity.setText(String.valueOf(quantity));
                if (onQuantityChangeListener != null)
                    onQuantityChangeListener.onQuantityChanged(quantity, false);
            }
        } else if (v == mButtonRemove) {
            if (quantity - 1 < minQuantity) {
                if (onQuantityChangeListener != null) onQuantityChangeListener.onLimitReached();
            } else {
                quantity -= 1;
                mTextViewQuantity.setText(String.valueOf(quantity));
                if (onQuantityChangeListener != null)
                    onQuantityChangeListener.onQuantityChanged(quantity, false);
            }
        } else if (v == mTextViewQuantity) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setTitle("Change Quantity");

            View inflate = LayoutInflater.from(getContext()).inflate(R.layout.qv_dialog_changequantity, null, false);
            final EditText et = (EditText) inflate.findViewById(R.id.qv_et_change_qty);
            et.setText(String.valueOf(quantity));

            builder.setView(inflate);
            builder.setPositiveButton("Change", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String newQuantity = et.getText().toString();
                    if (isNumber(newQuantity)) {
                        int intNewQuantity = Integer.parseInt(newQuantity);
                        setQuantity(intNewQuantity);
                    }
                }
            }).setNegativeButton("Cancel", null);
            builder.show();
        }
    }


    public OnQuantityChangeListener getOnQuantityChangeListener() {
        return onQuantityChangeListener;
    }

    public void setOnQuantityChangeListener(OnQuantityChangeListener onQuantityChangeListener) {
        this.onQuantityChangeListener = onQuantityChangeListener;
    }

    public Drawable getQuantityBackground() {
        return quantityBackground;
    }

    public void setQuantityBackground(Drawable quantityBackground) {
        this.quantityBackground = quantityBackground;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            mTextViewQuantity.setBackground(quantityBackground);
        } else {
            mTextViewQuantity.setBackgroundDrawable(quantityBackground);
        }
    }

    public Drawable getAddButtonBackground() {
        return addButtonBackground;
    }

    public void setAddButtonBackground(Drawable addButtonBackground) {
        this.addButtonBackground = addButtonBackground;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            mButtonAdd.setBackground(addButtonBackground);
        } else {
            mButtonAdd.setBackgroundDrawable(addButtonBackground);
        }
    }

    public Drawable getRemoveButtonBackground() {
        return removeButtonBackground;
    }

    public void setRemoveButtonBackground(Drawable removeButtonBackground) {
        this.removeButtonBackground = removeButtonBackground;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            mButtonRemove.setBackground(removeButtonBackground);
        } else {
            mButtonRemove.setBackgroundDrawable(removeButtonBackground);
        }
    }

    public String getAddButtonText() {
        return addButtonText;
    }

    public void setAddButtonText(String addButtonText) {
        this.addButtonText = addButtonText;
        mButtonAdd.setText(addButtonText);
    }

    public String getRemoveButtonText() {
        return removeButtonText;
    }

    public void setRemoveButtonText(String removeButtonText) {
        this.removeButtonText = removeButtonText;
        mButtonRemove.setText(removeButtonText);
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        boolean limitReached = false;

        if (quantity > maxQuantity) {
            quantity = maxQuantity;
            limitReached = true;
            if (onQuantityChangeListener != null) onQuantityChangeListener.onLimitReached();
        }
        if (quantity < minQuantity) {
            quantity = minQuantity;
            limitReached = true;
            if (onQuantityChangeListener != null) onQuantityChangeListener.onLimitReached();
        }


        if (!limitReached && onQuantityChangeListener != null)
            onQuantityChangeListener.onQuantityChanged(quantity, true);

        this.quantity = quantity;
        mTextViewQuantity.setText(String.valueOf(this.quantity));
    }

    public int getMaxQuantity() {
        return maxQuantity;
    }

    public void setMaxQuantity(int maxQuantity) {
        this.maxQuantity = maxQuantity;
    }

    public int getMinQuantity() {
        return minQuantity;
    }

    public void setMinQuantity(int minQuantity) {
        this.minQuantity = minQuantity;
    }

    public int getQuantityPadding() {
        return quantityPadding;
    }

    public void setQuantityPadding(int quantityPadding) {
        this.quantityPadding = quantityPadding;
        mTextViewQuantity.setPadding(quantityPadding, 0, quantityPadding, 0);
    }

    public int getQuantityTextColor() {
        return quantityTextColor;
    }

    public void setQuantityTextColor(int quantityTextColor) {
        this.quantityTextColor = quantityTextColor;
        mTextViewQuantity.setTextColor(quantityTextColor);
    }

    public void setQuantityTextColorRes(int quantityTextColorRes) {
        this.quantityTextColor = ContextCompat.getColor(getContext(), quantityTextColorRes);
        mTextViewQuantity.setTextColor(quantityTextColor);
    }

    public int getAddButtonTextColor() {
        return addButtonTextColor;
    }

    public void setAddButtonTextColor(int addButtonTextColor) {
        this.addButtonTextColor = addButtonTextColor;
        mButtonAdd.setTextColor(addButtonTextColor);
    }

    public void setAddButtonTextColorRes(int addButtonTextColorRes) {
        this.addButtonTextColor = ContextCompat.getColor(getContext(), addButtonTextColorRes);
        mButtonAdd.setTextColor(addButtonTextColor);
    }

    public int getRemoveButtonTextColor() {
        return removeButtonTextColor;
    }

    public void setRemoveButtonTextColor(int removeButtonTextColor) {
        this.removeButtonTextColor = removeButtonTextColor;
        mButtonRemove.setTextColor(removeButtonTextColor);
    }

    public void setRemoveButtonTextColorRes(int removeButtonTextColorRes) {
        this.removeButtonTextColor = ContextCompat.getColor(getContext(), removeButtonTextColorRes);
        mButtonRemove.setTextColor(removeButtonTextColor);
    }

    private int dpFromPx(final float px) {
        return (int) (px / getResources().getDisplayMetrics().density);
    }

    private int pxFromDp(final float dp) {
        return (int) (dp * getResources().getDisplayMetrics().density);
    }


    private boolean isNumber(String string) {
        try {
            Integer.parseInt(string);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


}


qv_dialog_changequantity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/qv_et_change_qty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:inputType="number" />

</LinearLayout>


attr.xml
<resources>
    <declare-styleable name="QuantityView">
        <attr name="qv_addButtonText" format="string" />
        <attr name="qv_addButtonBackground" format="color|reference" />
        <attr name="qv_addButtonTextColor" format="color" />

        <attr name="qv_removeButtonText" format="string" />
        <attr name="qv_removeButtonBackground" format="color|reference" />
        <attr name="qv_removeButtonTextColor" format="color" />

        <attr name="qv_quantity" format="integer" />
        <attr name="qv_quantityBackground" format="color|reference" />
        <attr name="qv_quantityTextColor" format="color" />
        <attr name="qv_quantityPadding" format="dimension" />

        <attr name="qv_maxQuantity" format="integer" />
        <attr name="qv_minQuantity" format="integer" />
    </declare-styleable>
</resources>


string.xml
<string name="qv_add">+</string>
<string name="qv_remove">-</string>


drawable:
qv_bg_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#ddd" />
        </shape>
    </item>
    <item android:state_selected="true">
        <shape android:shape="rectangle">
            <solid android:color="#ddd" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#eee" />
        </shape>
    </item>

</selector>


qv_btn_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#ccc" />
        </shape>
    </item>
    <item android:state_selected="true">
        <shape android:shape="rectangle">
            <solid android:color="#ccc" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#ddd" />
        </shape>
    </item>

</selector>
  • 大小: 25.6 KB
分享到:
评论

相关推荐

    QTableView 插入按钮等各种控件实例

    1. **创建自定义模型**:继承`QAbstractTableModel`,并重写必要的方法,如`data()`, `flags()`和`setData()`,以支持编辑和显示带有控件的单元格。 2. **自定义视图**:可能需要创建一个继承自`QTableView`的类,...

    MFC 带下拉选择列表的按钮控件

    总结来说,创建一个带下拉选择列表的按钮控件,需要对MFC的控件派生、消息处理以及Windows API有深入的理解。通过这样的实践,你可以扩展MFC的基本功能,为用户提供更加丰富和个性化的界面体验。

    VB按钮控件大全,多个漂亮的VB按钮

    例如,可以使用有图标的按钮来区分不同的功能,使用带有动态效果的按钮来增加交互的趣味性,或者使用特殊功能的按钮来简化复杂的操作流程。 总的来说,"VB按钮控件大全"是一个强大的资源库,为VB开发者提供了更多...

    VB按钮美化控件

    在VB(Visual Basic)编程环境中,开发者经常需要创建和使用各种控件,其中包括按钮控件。按钮是用户界面中常见的元素,用于触发特定的操作或事件。然而,VB默认的按钮样式可能较为简单,无法满足一些高级用户界面...

    WPF带时间的日期选择控件

    2. 使用第三方库:有一些开源库如Telerik、DevExpress等提供了扩展的UI控件,其中包括带有时间选择功能的日期选择器。这些库通常提供了丰富的定制选项和良好的用户体验。 3. XAML与C#结合:开发者可能利用XAML来...

    Labview按钮自定义控件素材(png图片)

    利用这些PNG素材,开发者可以创建响应式按钮,使按钮在不同交互状态下显示不同的图像,增加用户与程序的互动性。 6. **提高代码可读性**:自定义控件的使用也能帮助提高代码的可读性。通过直观的图形,开发者和团队...

    C# 自定义按钮控件

    C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮...

    ios-购物车数字选择控件.zip

    首先,购物车数字选择控件通常由两个部分组成:一个加号按钮和一个减号按钮,中间显示当前选中的商品数量。用户可以通过点击加号或减号按钮来增加或减少商品数量。这种设计简洁直观,便于用户操作。 在iOS中,我们...

    c#实现圆形按钮控件

    控件是用户界面的基本构建块,如按钮、文本框等,它们提供了与用户交互的功能。在.NET Framework中,这些控件是Windows Forms或WPF(Windows Presentation Foundation)的一部分。自定义控件则是程序员根据需求扩展...

    unigu加减控件带加减按钮.rar

    对于"unigu加减控件带加减按钮.rar"这个压缩包,我们可以推断其内容可能是一个用Delphi开发的用户界面组件库,特别是一个包含加减按钮的控件,常用于数量增减或数值调整的场景,如购物车、计数器等。 这个控件很...

    android 带加减按钮 EditText控件

    本文将深入探讨如何在Android中创建一个带有加减按钮的EditText控件,该控件常用于输入数量或者评分等场景。我们将分析需求、设计思路,并通过具体的代码实现来详细讲解。 需求分析: 1. 在EditText控件的左右两侧...

    C#开关 仪表 按钮等控件

    在.NET框架中,WinForms(Windows Forms)是用于构建桌面应用程序的一个强大工具,它提供了丰富的用户界面元素,如开关、仪表和按钮等控件。这些控件使得开发者能够创建交互性强、功能丰富的应用。在本主题中,我们...

    TabControl控件带关闭按钮和多种样式

    本教程将深入探讨如何通过自定义用户控件来实现一个带有关闭按钮和多种样式的`TabControl`。 首先,我们需要创建一个新的用户控件(UserControl)。在VB.NET项目中,右键点击“控件”或“窗体”文件夹,选择“添加...

    增加减少选择控件

    在Android应用开发中,用户界面的设计是至关重要的,其中一种常见的交互元素是“增加减少选择控件”,通常用于商品数量的选择、评分等场景。本文将深入解析标题为“增加减少选择控件”的源码案例——QuantityView,...

    Android-使用kotlin开发的数量控制控件一般用于商城类app加减商品数量

    本教程将深入探讨如何使用Kotlin语言来开发这样一个数量控制控件,以实现用户能够轻松增加或减少购物车中商品的数量。 首先,我们需要创建一个新的`QuantityControlView`类,它将继承自Android的`LinearLayout`或者...

    带副标题的水晶按钮、包括了控件的写法、GDI+带副标题的水晶按钮、包括了控件的写法、GDI+带副标题的水晶按钮、包括了控件的写法、GDI+

    带副标题的水晶按钮、包括了控件的写法、GDI+带副标题的水晶按钮、包括了控件的写法、GDI+带副标题的水晶按钮、包括了控件的写法、GDI+带副标题的水晶按钮、包括了控件的写法、GDI+带副标题的水晶按钮、包括了控件的...

    vc++ MFC 界面设计编程 各种控件设计,包括树控件,视图控件,静态文本,按钮控件,旋转控件,组合狂,tab控件等等

    旋转控件常与编辑框一起使用,提供一个向上/向下箭头,用于增加或减少数值输入。CSpinButtonCtrl类提供了与这些箭头交互的方法,如OnSpinUp和OnSpinDown事件处理函数。 6. **组合框** (CComboBox): 组合框结合了...

    C#自定义控件-三角形按钮

    本示例中的“C#自定义控件-三角形按钮”是一个创新的设计,它将传统的按钮形状转变为三角形,以满足非传统布局或设计要求。下面我们将深入探讨这个主题,包括如何创建自定义控件、实现三角形形状以及如何应用到项目...

    10款VB6的OCX按钮控件

    本资源提供了10款适用于VB6的OCX(ActiveX Control)按钮控件,这些控件在经过测试后被证明功能正常,样式多样,可以为开发者带来更多的设计选择。 OCX是Microsoft开发的一种组件技术,它基于OLE(Object Linking ...

    WPF控件 文件路径选择、文件夹选择

    与文件路径选择类似,我们可以使用`FolderBrowserDialog`控件。在XAML中,你可能不会直接声明这个控件,因为它是在代码-behind中使用的。以下是如何实现它的示例: ```csharp using System.Windows; using System....

Global site tag (gtag.js) - Google Analytics