Android中限制EditText输入数字范围

说明:

android中要求输入框中只能输入有符号数字,并且限制范围,下面给出要求,例子和函数

有需要可以借鉴。

要求:

  1. 输入整数数字,范围-180~180。
  2. 首字符一定是非零数字或-
  3. 中间不能有-

代码:

界面

    <EditText
        android:id="@+id/et_item_left"
        android:layout_width="10dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="@dimen/dp_1"
        android:layout_weight="1"
        android:background="#ffffff"
        android:digits="-0123456789"
        android:gravity="center"
        android:imeOptions="actionNext"
        android:inputType="numberSigned"
        android:maxLength="4"
        android:maxLines="1"
        android:text="-100"
        android:textColor="#333333"
        android:textSize="@dimen/sp_13"
        tools:maxLength="10"
        tools:text="数字限制例子" />

用法

           EditText etItemRight = itemView.findViewById(R.id.et_item_right);
           EtLimit(etItemLeft, -180, 180,(editable)->{info.setLeft(Text2Int(editable.toString()));});

函数

public static void EtLimit(EditText et, int maxValue) {
        EtLimit(et, 0, maxValue, null);
    }

    public static void EtLimit(EditText et, int minValue, int maxValue) {
        EtLimit(et, minValue, maxValue, null);
    }

    public static void EtLimit(EditText et, int maxValue, TextViewBindingAdapter.AfterTextChanged textChanged) {
        EtLimit(et, 0, maxValue, textChanged);
    }

    public static void EtLimit(EditText et, int minValue, int maxValue, TextViewBindingAdapter.AfterTextChanged textChanged) {
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            @Override
            public void afterTextChanged(Editable arg0) {
                String content = arg0.toString();
                if (content.length() > 0) {
                    if (content.length() == 1 && content.startsWith("-")) {
                        return;
                    }

                    if (content.length() > 1 && content.startsWith("0")) {
                        arg0.replace(0, 1, "");
                        return;
                    }
                    if (content.length() > 2 && content.startsWith("-0")) {
                        arg0.replace(1, 2, "");
                        return;
                    }
                    Integer num;
                    try {
                        num = Integer.parseInt(content);
                    } catch (NumberFormatException e) {
                        content = content.substring(1).replace("-", "");
                        Editable ab = new SpannableStringBuilder(content);
                        arg0.replace(1, arg0.length(), ab);
                        return;
                    }

                    if (num > maxValue || num < minValue) {
                        if (content.length() >= 1) {
                            arg0.delete(content.length() - 1, content.length());
                        }
                    } else {
                        if (textChanged != null) {
                            textChanged.afterTextChanged(arg0);
                        }
                    }
                }
            }
        });
    }

关于:

编者:李国帅

qq:9611153 微信lgs9611153

时间:2021-10-31  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微澜-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值