Skip to content Skip to sidebar Skip to footer

How To Get The Last Character Typed In A `edittext`?

I want to know when the user types a space character so that I know to update the undo list. Using onKeyListener does not work on virtual keyboards (and is conceptually the wrong a

Solution 1:

Try this: It should take care of it for you. You may want to add a bit more error checking.

edittext.addTextChangedListener(newTextWatcher() {
    @OverridepublicvoidbeforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @OverridepublicvoidonTextChanged(final CharSequence s, int start, int before, int count) {
        //Char at newly inserted pos.charcurrentChar= s.charAt(start + before);  
    }

    @OverridepublicvoidafterTextChanged(Editable editable) {

    }
});

Solution 2:

You can compare strings from before and after the TextChanged event so that you can see what has changed. This function returns the last char typed or deleted.

oldStr is set in beforeTextChanged and newStr in onTextChanged.

This additionally returns the last deleted character.

public char lastCharTyped(String oldStr, String newStr){
    String shorter, longer;
    //find which string is shorter so that we don't end up out of boundsif (oldStr.length()<newStr.length() ) {
        shorter = oldStr;
        longer = newStr;
    } else {
        shorter = newStr;
        longer = oldStr;
    }

    //find the first character that is different and return itfor (int i = 0; i < shorter.length(); i++) {
        if (longer.charAt(i) != shorter.charAt(i)) return longer.charAt(i);
    }

    //if no characters are different then return the last char of the longer string//this means that the undo will be saved if the last char the user erased is a spaceLog.d("longer",longer);
    return(longer.charAt(longer.length()-1));
}

Solution 3:

Here is an answer to 'do something' when a '#'(special character) is entered in text.

editText.addTextChangedListener(newTextWatcher() {
        
        @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
            //do nothing
        }

        @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
            //do nothing
        }

        @OverridepublicvoidafterTextChanged(Editable s) {
            Stringstring= s.toString();
                if(string.contains("#")){
                    //do something
          }
        }

    });

Solution 4:

et1.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        // TODO Auto-generated method stub
    }

    @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
    }
});

Post a Comment for "How To Get The Last Character Typed In A `edittext`?"