EditText内容输入限制最大:小数点前五位,小数点后2位

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

    String temp = edt.toString();
    int posDot = temp.indexOf(".");//返回指定字符在此字符串中第一次出现处的索引
    if (posDot <= 0) {//不包含小数点
        if (temp.length() <= 5) {
            return;//小于五位数直接返回
        } else {
            edt.delete(5, 6);//大于五位数就删掉第六位(只会保留五位)
            return;
        }
    }
    if (temp.length() - posDot - 1 > 2)//如果包含小数点
    {
        edt.delete(posDot + 3, posDot + 4);//删除小数点后的第三位
    }
}

猜你喜欢

转载自blog.csdn.net/briup_qiuqiu/article/details/51908160