EditText小数点以下の桁数の制限

まず、プログラム:
@ 1、addTextChangedListener 
短所:予約指定された数の後に、さらに他の追加TextChangedListenerを実行します

@ 2、InputFilter
完璧なソリューションは、欠点のaddTextChangedListenerを達成するために
第二に、認識に基づいてInputFilter
1、InputFilterツール

/ ** 
 * ggband 
 *分数および整数の限られた数
 * / 
実装InputNumLengthFilterパブリッククラスInputFilter { 

    プライベートINT MaxPoint; 
    プライベートint型maxInteger; 
    公共InputNumLengthFilter(MaxPoint INT、INT maxInteger){ 
        this.maxPoint = MaxPoint; 
        this.maxInteger = maxInteger ; 
    } 

    @Override 
    公共のCharSequenceフィルタ(のCharSequenceソース、int型開始、終了のintは、DEND INT、DSTARTはint、DESTスパン){ 
        int型のmaxLength = maxInteger MaxPoint + + 1;。
        特殊文字は//のように、直接的なリターンを削除する
        場合(nullFilter(ソース))戻りNULL; 
        列dValue dest.toString =();
        文字列[] splitArray = dValue.split( "\\。"); //在点前后分开两段
        IF(splitArray.length> 0){ 
            文字列intValue = splitArray [0]。
            int型誤りインデックス= dValue.indexOf( ""); 
            IF(誤りインデックス== -1){ 
                誤りインデックス= dValue.length()。
            } 
            IF(intValue.length()> = maxLengthの- maxPoint - 1 && DSTART <=誤りインデックス){ 
                IF( ""。等号(source.toString())){ 
                    戻りヌル。
                } 
                「」を返します。
            } 
        } 
        もし(splitArray.length> 1 && DSTART == dValue.length()){ 
            int型のdiff = dotValue.length()+ 1 - maxPoint。
            もし(差分> 
                試し{ 
                    リターンsource.subSequence(スタート、エンド-デフ); 
                }キャッチ(例外:IndexOutOfBoundsException E){ 
                    リターン出典; 
                } 
            } 
        } 
        IF(dest.length()==のmaxLength "" - 1 &&等号(source.toString())){ 
            リターン""; 
        } 
        IF(dest.length()> = maxLengthの){ 
            リターン""; 
        } 
        戻りNULL; 
    } 

    //最初のパラメータであります小数の桁数は、第2のパラメータは、(小数点を含む)全体の長さである

    プライベートブールnullFilter(たCharSequenceソース){
       。source.toString()のisEmpty()を返します。
    } 
}

  

 2、なViewHelperツール:

/ ** 
 *許可入力ボックスがビット整数桁数と小数点以下の指定した数のみを入力することができます
 * 
 * @paramのEditTextのEditText 
 * @param maxInteger最大の整数桁数
 * @param maxPoint小数点以下の桁数の最大数
 *作成ggbandによって
 * / 
パブリック静的な無効setPricePointWithInteger (最終のEditTextのEditText、最終MaxPoint int型、最終maxInteger、InputFilter ... inputFilters INT){ 
    IF(inputFilters == NULL || inputFilters.length == 0){ 
        editText.setFilters(InputFilter新しい新規[] {新規新しいInputNumLengthFilter(MaxPoint、 maxInteger)}); 
    } {他
        InputFilter [] =新しい新しいnewInputFilters InputFilter [inputFilters.length + 1]; 
        System.arraycopyの(inputFilters、0、newInputFilters、0、inputFilters.length)。
        newInputFilters [inputFilters.length] =新しいInputNumLengthFilter(maxPoint、maxInteger)。
        editText.setFilters(newInputFilters)。
    } 

}
3、使用:
ViewHelper.setPricePointWithInteger(etCpu、4、10)

  

おすすめ

転載: www.cnblogs.com/ggband/p/11815897.html