zoukankan      html  css  js  c++  java
  • android edittext 限制小数点后最多只能输入两位数字

        android:inputType="numberDecimal"

    private InputFilter lengthFilter = new InputFilter() {
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
    // source:当前输入的字符
    // start:输入字符的开始位置
    // end:输入字符的结束位置
    // dest:当前已显示的内容
    // dstart:当前光标开始位置
    // dent:当前光标结束位置
    LogUtil.i("", "source=" + source + ",start=" + start + ",end=" + end + ",dest=" + dest.toString() + ",dstart=" + dstart + ",dend=" + dend);
    if (dest.length() == 0 && source.equals(".")) {
    return "0.";
    }
    String dValue = dest.toString();
    String[] splitArray = dValue.split("\.");
    if (splitArray.length > 1) {
    String dotValue = splitArray[1];
    if (dotValue.length() == 2) {//输入框小数的位数
    return "";
    }
    }
    return null;
    }
    };

    edit.setFilters(new InputFilter[]{lengthFilter});




    class   MyInputFilter implements InputFilter{
    public MyInputFilter(int dotLength) {
    this.dotLength = dotLength;
    }

    int dotLength ;

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
    // source:当前输入的字符
    // start:输入字符的开始位置
    // end:输入字符的结束位置
    // dest:当前已显示的内容
    // dstart:当前光标开始位置
    // dent:当前光标结束位置
    LogUtil.i("", "source=" + source + ",start=" + start + ",end=" + end + ",dest=" + dest.toString() + ",dstart=" + dstart + ",dend=" + dend);
    if (dest.length() == 0 && source.equals(".")) {
    return "0.";
    }
    String dValue = dest.toString();
    String[] splitArray = dValue.split("\.");
    if (splitArray.length > 1) {
    String dotValue = splitArray[1];
    if (dotValue.length() == dotLength) {//输入框小数的位数
    return "";
    }
    }
    return null;
    }
    }

  • 相关阅读:
    HTML 标签元素的 align 属性
    JS计算时间差值
    ICPC Southeastern Europe Contest 2019 BDFIJ
    ICPC Latin American Regional Contests 2019 EGIKLM
    UCF Local Programming Contest 2017 ABCDEFGHI
    Codeforces #631 Dreamoon Likes Coloring
    Problem Palindrome
    Problem Toki’s function
    UCF “Practice” Local Contest — Aug 25, 2018 Boots Exchange 水题
    UCF “Practice” Local Contest — Aug 25, 2018 Rummy Score
  • 原文地址:https://www.cnblogs.com/lucktian/p/6308519.html
Copyright © 2011-2022 走看看