android控件ediittext输入类型

时间:2015-03-16 10:46:21

标签: android android-edittext android-inputtype android-input-filter

我有一个edittext,我想控制它。输入类型是numberDecimal,我想写这样的验证。我希望在十进制之前应该是0>和最大值10并且在十进制之后0>最多2个号码 我搜索了一下,并找到了一些InputFilter示例。

edittext.setFilters(new InputFilter[] { new DigitsKeyListener(
            Boolean.FALSE, Boolean.TRUE) {
        int beforeDecimal = 10, afterDecimal = 2;

        @Override
        public CharSequence filter(CharSequence source, int start, int end,
                Spanned dest, int dstart, int dend) {
            String etText = edittext.getText().toString();
            String temp = edittext.getText() + source.toString();
            if (temp.equals(".")) {
                return "0.";
            } else if (temp.toString().indexOf(".") == -1) {
                // no decimal point placed yet
                if (temp.length() > beforeDecimal) {
                    return "";
                }
            } else {
                int dotPosition ;
                int cursorPositon = edittext.getSelectionStart();
                if (etText.indexOf(".") == -1) {
                    Log.i("First time Dot", etText.toString().indexOf(".")+" "+etText);
                    dotPosition = temp.indexOf(".");
                    Log.i("dot Positon", cursorPositon+"");
                    Log.i("dot Positon", etText+"");
                    Log.i("dot Positon", dotPosition+"");
                }else{
                    dotPosition = etText.indexOf(".");
                    Log.i("dot Positon", cursorPositon+"");
                    Log.i("dot Positon", etText+"");
                    Log.i("dot Positon", dotPosition+"");
                }
                if(cursorPositon <= dotPosition){
                    Log.i("cursor position", "in left");
                    String beforeDot = etText.substring(0, dotPosition);
                    if(beforeDot.length()<beforeDecimal){
                        return source;
                    }else{
                        if(source.toString().equalsIgnoreCase(".")){
                            return source;
                        }else{
                            return "";
                        }

                    }
                }else{
                    Log.i("cursor position", "in right");
                    temp = temp.substring(temp.indexOf(".") + 1);
                    if (temp.length() > afterDecimal) {
                        return "";
                    }
                }
            }

            return super.filter(source, start, end, dest, dstart, dend);
        }
    } });

但我不知道解决方案。

1 个答案:

答案 0 :(得分:0)

可以使用

以编程方式设置
setRawInputType(InputType.InputType.TYPE_NUMBER_FLAG_DECIMAL)  

setRawInputType(InputType.InputType.TYPE_CLASS_NUMBER)

反对edittext