Android商城 带加减按钮的 EditText

最近在做一个商城的项目,商城项目中需要选择商品数量,除了手动输入商品数量之外,还需要两个按钮可以增减商品数量,具体效果如下图。



本例说明的是上图中数量的增减和输入功能,考虑到商城项目,都会有个购物车模块,在购物车模块可能还会有更改商品数量的操作,也就是说这个修改商品数量的功能,我们需要考虑能不能复用的问题。

我的实现思路为自定义View,也就是说将左边的 “-” 按钮 中间的EditText 和最右边的 “+” 组合起来定义成一个View,这样我们在购买商品的界面,和后面购物车编辑的界面,就可以复用了。


OK,话不多说,我们直接上代码


这个是自定义View 的布局文件代码:

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    <span style="color:#ff0000;">android:background="@drawable/order_edit_bg"</span>
    android:orientation="horizontal"

    >

    <!-- 左边的 “-” 按钮 -->
    <RelativeLayout
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp">

        <ImageView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="<span style="color:#ff0000;">@drawable/order_delete</span>"/>
    </RelativeLayout>
    <!-- 画线 -->
    <ImageView
        android:layout_width="@dimen/order_edit_width"
        android:layout_height="match_parent"
        android:background="<span style="color:#ff0000;">@color/order_edit_line</span>"/>

    <EditText
        android:id="@+id/edit"
        android:layout_width="40dp"
        android:layout_height="25dp"
        android:background="@null"
        android:inputType="number"
        android:maxLines="1"
        android:paddingLeft="3dp"
        android:textColor="@color/goods_name"
        android:gravity="center"
        android:textSize="15sp"
        android:focusable="true"
        />

    <ImageView
        android:layout_width="@dimen/order_edit_width"
        android:layout_height="match_parent"
        android:background="@color/order_edit_line"/>

    <!-- 右边的 “+” 按钮 -->
    <RelativeLayout
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp">

        <ImageView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="<span style="color:#ff0000;">@drawable/order_add</span>"/>
    </RelativeLayout>
</LinearLayout></span>

上面布局文件中有些引用(也就是红色部分),因为是真是项目,不凡便全部展示,只截取本例需要的给大家展示(主要是考虑到很多和我一样的Android小白)

<span style="color:#ff0000;">android:background="@drawable/order_edit_bg"</span><span style="color:#333333;"> 需要在 drawable目录下创建一个名为:order_edit_bg.xml的配置文件,该文件具体定义如下:</span>
<span style="color:#333333;"></span><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke android:color="@color/order_edit_line" android:width="@dimen/order_edit_width"/>
    <solid android:color="@color/white"/>
</shape>

 

<span style="font-size:14px;">@drawable/order_delete 图片给你们:</span>
<span style="font-size:14px;"><img src="https://img-blog.csdn.net/20160516000856627" alt="" />
</span>
<span style="font-size:14px;"></span><pre name="code" class="html"><span style="font-size:14px;">@color/order_edit_line : </span><pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Menlo';font-size:12.0pt;"><span style="color:#e8bf6a;"><color </span><span style="color:#bababa;">name=</span><span style="color:#a5c261;">"order_edit_line"</span><span style="color:#e8bf6a;">></span>#8a8a8a<span style="color:#e8bf6a;"></color></span>

 
 


<span style="font-size:14px;"><span style="color:#ff0000;">@drawable/order_add</span> 图片如下:</span>
<span style="font-size:14px;"><img src="https://img-blog.csdn.net/20160516001100627" alt="" />
</span>


好了,我们的布局文件已经说完了,接下类就是我们的自定义View类,具体代码如下:

/**
 * 带±的输入框 Created by BscJhNN on 15/12/3.
 */
public class OrderEditTextView extends FrameLayout implements View.OnClickListener {

    private View add;

    private View del;

    private EditText edit;

    private OnOrderEditCallBack callback;

    //添加商品是默认的最大添加数量
    private int max = 255;

    private final int MIN = 1;

    public boolean canToast = true;

    /**
     * 申请退货提示
     */
    private boolean isReturnGoodsToast;

    public OrderEditTextView(Context context) {
        super(context);
        init();
    }

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

    public void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.order_edit, null);
        this.addView(view);
        add = findViewById(R.id.add);
        del = findViewById(R.id.delete);
        edit = (EditText) findViewById(R.id.edit);
        edit.setText("1");
        add.setOnClickListener(this);
        del.setOnClickListener(this);
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                int num = Integer.parseInt(TextUtils.isEmpty(edit.getText().toString()) ? "1"
                        : edit.getText().toString());
                if (max < MIN) {
                    max = MIN;
                }
                if (num > max) {
                    edit.setText(String.valueOf(max));
                    if (edit.getVisibility() == View.VISIBLE) {
                        if (canToast) {
                            Toast.makeText(getContext(), "已到达商品最大购买数量", Toast.LENGTH_SHORT).show();
                        }

                    }

                } else if (num < MIN) {
                    edit.setText(String.valueOf(MIN));
                }

                if (callback != null) {
                    callback.OnCallBack(getText());
                }
            }
        });

    }

    public void setRetrunGoodsToast(boolean isReturnGoodsToast) {
        this.isReturnGoodsToast = isReturnGoodsToast;
    }

    @Override
    public void onClick(View view) {
        int num = Integer.parseInt(
                TextUtils.isEmpty(edit.getText().toString()) ? "1" : edit.getText().toString());
        switch (view.getId()) {
            case R.id.add:
                if (num < max) {
                    edit.setText(String.valueOf(num + 1));
                } else {
                    if (!isReturnGoodsToast) {
                        Toast.makeText(getContext(), "已达到商品可购买最大数量了哦", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getContext(), "已达到可申请最大数量了哦", Toast.LENGTH_SHORT).show();
                    }

                }
                break;
            case R.id.delete:
                int n = num - 1;
                if (num > MIN) {
                    edit.setText(String.valueOf(num - 1));
                }
                if (n < MIN) {
                    Toast.makeText(getContext(), "不能再减少啦", Toast.LENGTH_SHORT).show();
                }
                break;
        }

        edit.setSelection(edit.getText().length());
    }

    public int getText() {
        try {
            return Integer.parseInt(edit.getText().toString());
        } catch (Exception e) {
            return 1;
        }
    }

    public void setText(String text) {
        try {
            edit.setText(TextUtils.isEmpty(text) ? "1" : String.valueOf(Integer.parseInt(text)));
        } catch (Exception e) {
            edit.setText("1");
        }
    }

    public void setOnOrderEditCallBack(OnOrderEditCallBack callback) {
        this.callback = callback;
    }

    /**
     * 设置edit最大值
     *
     * @param max
     */
    public void setMax(int max) {
        this.max = max;
    }

    /**
     * 点击事件的回调
     */
    public interface OnOrderEditCallBack {
        void OnCallBack(int num);
    }
}

上面代码大家仔细研读,大神请骗过,奉献给和我一起处于Android开发初期的兄弟们。

有什么问题欢迎大家和我交流,我的QQ:290887654

第一次下决心要写博客,写的不通顺的地方,欢迎大家指出。









  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现这个功能,可以自定义一个加减按钮的布局,然后将其作为EditText的左右drawable,并设置相应的点击事件。 以下是一个简单的实现方法: 1. 自定义一个加减按钮的布局,例如: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <ImageButton android:id="@+id/btn_minus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_minus" android:background="@null" /> <EditText android:id="@+id/et_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="number" android:text="0" android:textAlignment="center" /> <ImageButton android:id="@+id/btn_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_add" android:background="@null" /> </LinearLayout> ``` 2. 在Activity或Fragment中,将该布局作为EditText的左右drawable,并设置相应的点击事件: ``` EditText editText = findViewById(R.id.edit_text); ViewGroup layout = findViewById(R.id.layout_number); ImageButton btnMinus = layout.findViewById(R.id.btn_minus); ImageButton btnAdd = layout.findViewById(R.id.btn_add); final EditText etNumber = layout.findViewById(R.id.et_number); editText.setCompoundDrawablesWithIntrinsicBounds(layout, null, null, null); btnMinus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int number = Integer.parseInt(etNumber.getText().toString()); if (number > 0) { number--; etNumber.setText(String.valueOf(number)); } } }); btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int number = Integer.parseInt(etNumber.getText().toString()); number++; etNumber.setText(String.valueOf(number)); } }); etNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus) { int number = Integer.parseInt(etNumber.getText().toString()); if (number < 0) { etNumber.setText("0"); } } } }); ``` 3. 在点击加减按钮时,根据当前EditText的值进行加减操作并更新EditText的显示。在失去焦点时,检查EditText的值是否小于0,如果小于0则将其设置为0。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值