android EditText限制只能输入2位小数的解决方法

来源:互联网 发布:大数据 智能交通 编辑:程序博客网 时间:2024/05/01 09:41
txtAmount = (EditText) findViewById(R.id.amount);        txtAmount.addTextChangedListener(new TextWatcher() {            @Override            public void onTextChanged(CharSequence s, int start, int before,                    int count) {                String text = s.toString();                if (text.contains(".")) {                    int index = text.indexOf(".");                    if (index + 3 < text.length()) {                        text = text.substring(0, index + 3);                        txtAmount.setText(text);                        txtAmount.setSelection(text.length());                    }                }            }            @Override            public void beforeTextChanged(CharSequence s, int start, int count,                    int after) {                // TODO Auto-generated method stub            }            @Override            public void afterTextChanged(Editable s) {                // TODO Auto-generated method stub            }        });

原创粉丝点击