Android EditText实现只能输入几位整数几位小数

EditText的TextWatcher监听几个回调方法的解释

        mEditText = (EditText) findViewById(R.id.edittext);
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.i("CustomActivity","beforeTextChanged");
                Log.i("beforeTextChanged","s = " + s);
                Log.i("beforeTextChanged","start = " + start);
                Log.i("beforeTextChanged","count = " + count);
                Log.i("beforeTextChanged","after = " + after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i("CustomActivity","onTextChanged");
                Log.i("onTextChanged","s = " + s);
                Log.i("onTextChanged","start = " + start);
                Log.i("onTextChanged","count = " + count);
                Log.i("onTextChanged","before = " + before);

            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.i("CustomActivity","afterTextChanged");
                Log.i("afterTextChanged","s = " + s);

            }
        });

当输入第一个字符”0”的时候,log如下

06-28 20:44:09.815 28820-28820/com.alfred.chowder I/CustomActivity: beforeTextChanged
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/beforeTextChanged: s = 
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/beforeTextChanged: start = 0
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/beforeTextChanged: count = 0
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/beforeTextChanged: after = 1
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/CustomActivity: onTextChanged
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/onTextChanged: s = 0
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/onTextChanged: start = 0
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/onTextChanged: count = 1
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/onTextChanged: before = 0
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/CustomActivity: afterTextChanged
06-28 20:44:09.815 28820-28820/com.alfred.chowder I/afterTextChanged: s = 0

当连续输入两个字符的时候(用搜狗输入法打”你好”,PS:记得连拼),log如下

06-28 20:51:50.415 28820-28820/com.alfred.chowder I/CustomActivity: beforeTextChanged
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/beforeTextChanged: s = 0
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/beforeTextChanged: start = 1
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/beforeTextChanged: count = 0
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/beforeTextChanged: after = 2
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/CustomActivity: onTextChanged
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/onTextChanged: s = 0你好
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/onTextChanged: start = 1
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/onTextChanged: count = 2
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/onTextChanged: before = 0
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/CustomActivity: afterTextChanged
06-28 20:51:50.419 28820-28820/com.alfred.chowder I/afterTextChanged: s = 0你好

分析如下

beforeTextChanged

  1. s : 之前输入的文字
  2. start : 当前输入第一个字符的下标,从0开始
  3. count : 不知道为何一直为0
  4. after : 本次输入的个数(但是看变量名不像啊(⊙o⊙)…)

onTextChanged

  1. s : 当前输入的所有文字(界面展示的文字)
  2. start : 当前输入的第一个字符的下标,从0开始
  3. count : 本次输入的个数
  4. before : 不知道为何一直为0

afterTextChanged

  1. s : 当前输入的所有文字(界面展示的文字) 与onTextChanged参数中的s值是一致的

代码实现 EditText只能输入三位整数两位小数(几位整数几位小数可以随意设置)

     * 整数位数
     */
    private static final int INTEGER_COUNT = 3;
    /**
     * 小数位数
     */
    private static final int DECIMAL_COUNT = 2;

    mEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.i("InputLimitActivity", "beforeTextChanged");
                Log.i("beforeTextChanged", "s = " + s);
                Log.i("beforeTextChanged", "start = " + start);
                Log.i("beforeTextChanged", "count = " + count);
                Log.i("beforeTextChanged", "after = " + after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i("InputLimitActivity", "onTextChanged");
                Log.i("onTextChanged", "s = " + s);
                Log.i("onTextChanged", "start = " + start);
                Log.i("onTextChanged", "count = " + count);
                Log.i("onTextChanged", "before = " + before);
                if (start == 0 && s.toString().equals(".") && count == 1) {
                    //输入的第一个字符为"."
                    mEditText.setText("");
                } else if (s.length() >= INTEGER_COUNT + 1 && count != 0) {
                    //当整数位数输入到达被要求的上限,并且当前在输入字符,而不是减少字符
                    if (s.toString().contains(".")) {
                        //当前输入的有"."字符
                        String[] text = s.toString().split("\\.");
                        if (text.length >= 2 && text[1].length() > DECIMAL_COUNT) {
                            //小数位数超数
//                          s.delete(text[0].length() + text[1].length() + 1, s.toString().length());
//                          mEditText.setText(s);
                            mEditText.setText(s.subSequence(0, s.toString().length() - 1));
                            mEditText.setSelection(s.toString().length() - 1);
                        }
                    } else {
//                      s.delete(s.toString().length() - 1, s.toString().length());
//                      mEditText.setText(s);
                        mEditText.setText(s.subSequence(0, s.toString().length() - 1));
                        mEditText.setSelection(s.toString().length() - 1);
                    }
                }

            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.i("InputLimitActivity", "afterTextChanged");
                Log.i("afterTextChanged", "s = " + s);


            }
        });

完美实现

实现的效果

只能输入一个小数,并且第一位不能输入为小数
只能输入三位整数,两位小数(这个可以自己配置)
当输入第四位整数时,不能显示在屏幕上,也就是输入不进去

但是这里有个问题

在afterTextChanged方法里面,我注释掉了四行代码,这是一种实现方式(但是超出范围的字符),但是这种方式抛出了StackOverFlowError,我不知道是为什么,有懂得请留言,在下先谢过[抱拳]!只能换一种实现方式来实现(String.subSequence()),最后实现的效果是一样的

你可能感兴趣的:(Android移动开发)