EditText 限制數(shù)字大小及小數(shù)位數(shù)

針對(duì)輸入框輸入金額有效位數(shù)限制

/**
 * EditText 數(shù)字輸入范圍過(guò)濾 (0 <= ? < MAX_VALUE && 小數(shù)點(diǎn)后保留 POINTER_LENGTH 位)
 *
 * @author gavin.xiong 2016/12/21
 */
public class NumRangeInputFilter implements InputFilter {

    // 只允許輸入數(shù)字和小數(shù)點(diǎn)
    private static final String REGEX = "([0-9]|\\.)*";
    // 輸入的最大金額  
    private static final int MAX_VALUE = 100000;
    // 小數(shù)點(diǎn)后的位數(shù)  
    private static final int POINTER_LENGTH = 2;

    private static final String POINTER = ".";

    private static final String ZERO_ZERO = "00";

    /**
     * @param source 新輸入的字符串
     * @param start  新輸入的字符串起始下標(biāo),一般為0
     * @param end    新輸入的字符串終點(diǎn)下標(biāo),一般為source長(zhǎng)度-1
     * @param dest   輸入之前文本框內(nèi)容
     * @param dstart 原內(nèi)容起始坐標(biāo),一般為0
     * @param dend   原內(nèi)容終點(diǎn)坐標(biāo),一般為dest長(zhǎng)度-1
     * @return 輸入內(nèi)容
     */
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        String sourceText = source.toString();
        String destText = dest.toString();

        // 新輸入的字符串為空(刪除剪切等)
        if (TextUtils.isEmpty(sourceText)) {
            return "";
        }

        // 拼成字符串
        String temp = destText.substring(0, dstart)
                + sourceText.substring(start, end)
                + destText.substring(dend, dest.length());
        Log.v("temp", "-" + temp);

        // 純數(shù)字加小數(shù)點(diǎn)
        if (!temp.matches(REGEX)) {
            Log.d("TAG", "!純數(shù)字加小數(shù)點(diǎn)");
            return dest.subSequence(dstart, dend);
        }

        // 小數(shù)點(diǎn)的情況
        if (temp.contains(POINTER)) {
            // 第一位就是小數(shù)點(diǎn)
            if (temp.startsWith(POINTER)) {
                Log.d("TAG", "第一位就是小數(shù)點(diǎn)");
                return dest.subSequence(dstart, dend);
            }
            // 不止一個(gè)小數(shù)點(diǎn)
            if(temp.indexOf(POINTER) != temp.lastIndexOf(POINTER)) {
                Log.d("TAG", "不止一個(gè)小數(shù)點(diǎn)");
                return dest.subSequence(dstart, dend);
            }
        }

        double sumText = Double.parseDouble(temp);
        if (sumText >= MAX_VALUE) {
            // 超出最大值
            Log.d("TAG", "超出最大值");
            return dest.subSequence(dstart, dend);
        }
        // 有小數(shù)點(diǎn)的情況下
        if (temp.contains(POINTER)) {
            //驗(yàn)證小數(shù)點(diǎn)精度,保證小數(shù)點(diǎn)后只能輸入兩位
            if (!temp.endsWith(POINTER) && temp.split("\\.")[1].length() > POINTER_LENGTH) {
                Log.d("TAG", "保證小數(shù)點(diǎn)后只能輸入兩位");
                return dest.subSequence(dstart, dend);
            }
        } else if (temp.startsWith(POINTER) || temp.startsWith(ZERO_ZERO)) {
            // 首位只能有一個(gè)0
            Log.d("TAG", "首位只能有一個(gè)0");
            return dest.subSequence(dstart, dend);
        }

        Log.d("TAG", "正常情況");
        return source;
    }
}  

在代碼中設(shè)置
editText.setFilters(new InputFilter[]{new NumRangeInputFilter()});

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,030評(píng)論 25 709
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評(píng)論 19 139
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,329評(píng)論 0 17
  • 今天早上到公司開各種會(huì),中午跟同事到新西南吃泰式火鍋,下午趕回公司錄單子,交資料,昨天簽署的家庭保單,累計(jì)為客戶送...
    卓彤的美好時(shí)光閱讀 159評(píng)論 0 0
  • 感謝敏媽分享! 1、先看清楚自己是誰(shuí) 2、不作不死 3、提升實(shí)力,爭(zhēng)取被勾搭 4、實(shí)現(xiàn)自我認(rèn)同 5、敢于奉獻(xiàn),提供...
    常思過(guò)閱讀 244評(píng)論 2 1

友情鏈接更多精彩內(nèi)容