Android 输入框监听输入数字

Android 输入框监听

可以配置整数位、小数位、可输入最小值

package com.chainplus.idasex.utils;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.chainplus.idasex.listener.TextWatchCallBack;


/**
 * desc:  输入框数字输入监听,几位小数几位整数
 * author:  yangtao
 * <p>
 * creat: 2018/8/19 19:05
 */

public class CountTextWatcher implements TextWatcher {

EditText editText;
TextWatchCallBack callBack;
//整数位
private int INTEGER_COUNT = 5;
//小数位
private int DECIMAL_COUNT = 2;

private double MIN_MARK = 0.01;

public CountTextWatcher(EditText editText, TextWatchCallBack callBack, int max1, int max2) {
    this.editText = editText;
    this.callBack = callBack;
    this.INTEGER_COUNT = max1;
    this.DECIMAL_COUNT = max2;
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {


}

@Override
public void onTextChanged(CharSequence s, int start, int before,
                          int count) {
    //设置最小值位MIN_MARK
    if (start > 2) {
        double num = Double.parseDouble(s.toString());
        //此处可设置最小值
        //if (num < MIN_MARK && s.toString().length() > 3) {
        //   s = String.valueOf(MIN_MARK);
        //   editText.setText(s);
        // }
        editText.setSelection(s.length());
    }

    //首位连续2个0剔除为0.
    if (s.toString().equals("00") || s.toString().equals("01") || s.toString().equals("02") || s.toString().equals("03")
            || s.toString().equals("04") || s.toString().equals("05") || s.toString().equals("06")
            || s.toString().equals("06") || s.toString().equals("07") || s.toString().equals("08") || s.toString().equals("09")) {
        editText.setText("0.");
        editText.setSelection(2);
    }
    if (s.toString().length() > 1 && !s.toString().contains(".") && s.toString().startsWith("0")) {
        editText.setText(s.subSequence(1, s.length()));
    }
}


@Override
public void afterTextChanged(Editable s) {
    judgeNumber(s, editText, INTEGER_COUNT, DECIMAL_COUNT);
    editText.setSelection(editText.getText().toString().length());
    callBack.onSuccess(editText);
}

/**
 * 金额输入框中的内容限制(最大:小数点前五位,小数点后2位)
 *
 * @param edt
 */
public static void judgeNumber(Editable edt, EditText editText, int max1, int max2) {

    String temp = edt.toString();
    int posDot = temp.indexOf(".");//返回指定字符在此字符串中第一次出现处的索引
    int secondDot = temp.indexOf(".", posDot + 1);
    int index = editText.getSelectionStart();//获取光标位置
    if (posDot == 0) {//必须先输入数字后才能输入小数点
        edt.delete(0, temp.length());//删除所有字符
        return;
    }

    if (posDot < 0) {//不包含小数点
        if (temp.length() <= max1) {
            return;//小于五位数直接返回
        } else {
            edt.delete(index - 1, index);//删除光标前的字符
            return;
        }
    }
    if (posDot > max1) {//小数点前大于5位数就删除光标前一位
        edt.delete(index - 1, index);//删除光标前的字符
        return;
    }
    //为整数  //此时不能再次在外层设置setSelection属性
    if (max2 == 0 && posDot > 0) {
        editText.setText(temp.substring(0, temp.length() - 1));//删除光标前的字符

        return;
    }
    //重复小数点
    if (secondDot != -1 && secondDot > posDot) {//小数点删除光标前一位小数点
        edt.delete(index - 1, index);//删除光标前的字符
        return;
    }

    if (temp.length() - posDot - 1 > max2 && index - 1 >= 0)//如果包含小数点
    {
        edt.delete(index - 1, index);//删除光标前的字符
        return;
    }


}
}

import android.widget.EditText;

/**
 * desc:  输入完回调接口
 * author:  yangtao
 * <p>
 * creat: 2018/8/21 16:05
 */

public interface TextWatchCallBack {
    void onSuccess(EditText editText);

}

使用方式:

getEditext().addTextChangedListener(new CountTextWatcher(inputQuantity.getEditext(), new TextWatchCallBack() {
        @Override
        public void onSuccess(EditText editText) {

        }
//整数位5位,小数位2位
    }, 5, 2));
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值