Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get which character is get deleted on backspace in android

I am working with android 2.2.

How to know which character is get deleted on backspace when editing text in custom auto complete in android.

public boolean onKeyUp(int keyCode,KeyEvent msg){  
     if(keyCode == KeyEvent.KEYCODE_DEL)
     {
        // how to find here which character is get deleted
     }
     return false;
}
like image 400
Smily Avatar asked Oct 21 '22 20:10

Smily


2 Answers

String prevText = "";
public boolean onKeyUp(int keyCode,KeyEvent msg){  

     if(keyCode == KeyEvent.KEYCODE_DEL)
     {
        int pos = myEditText.getSelectionStart();
        char c = prevText.charAt(pos);
        // c is deleted
     }
     prevText = myEditText.getText.toString();
     return false;
}
like image 163
Bobs Avatar answered Nov 01 '22 12:11

Bobs


You can use add a TextWatcher to AutoCompleteTextView with addTextChangeListener(TextWatcher).

You don't need to listen to onKeyUp() the various TextWatcher methods inform you if the user is adding or removing text.

like image 31
Sam Avatar answered Nov 01 '22 11:11

Sam