Live editing of users input

后端 未结 6 1235
半阙折子戏
半阙折子戏 2020-11-27 07:37

Is it possible to auto insert characters into an EditText as the user inputs data?

I.e. if the user is entering a long number such as <

6条回答
  •  猫巷女王i
    2020-11-27 07:48

    For those still facing trouble with backspace and multiple hyphens -

    new TextWatcher() 
    {
            boolean hyphenExists;
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (s.length() >= 6 && s.charAt(5) == '-') {
                    hyphenExists = true;
                } else {
                    hyphenExists = false;
                }
    
                Log.d("TAG", "beforeTextChanged " + s.toString());
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                Log.d("TAG", "onTextChanged " + s.toString());
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 5) {
                    if (!hyphenExists)
                        s.append('-');
                }
                Log.d("TAG", "afterTextChanged " + s.toString());
            }
        }
    

提交回复
热议问题