5

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;
}
Smily
  • 2,486
  • 4
  • 19
  • 31

6 Answers6

2
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;
}
Bobs
  • 21,870
  • 33
  • 134
  • 221
  • one more query for this ques. How to find color code of character which is get deleted on backspace. – Smily Jan 07 '13 at 10:56
1

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.

Sam
  • 84,460
  • 18
  • 172
  • 171
1

The easiest way is just keep last character that you type

int lastKeyCode;
public boolean onKeyUp(int keyCode,KeyEvent msg){  
     if(keyCode == KeyEvent.KEYCODE_DEL)
     {
        // print lastKeyCode here
        // how to find here which character is get deleted
     }
     lastKeyCode = keyCode;
     return false;
}
Sruit A.Suk
  • 6,247
  • 7
  • 55
  • 66
1

Try this, working for me

editText.addTextChangedListener(new TextWatcher() {
        String initialChar = null;
        int initCursorPosition = 0;

        @Override
        public void onTextChanged(CharSequence charSequence, int start, int before, int after) {
                char addedChar = 0;

                int finalCursorPosition = editText.getSelectionStart();
                if (finalCursorPosition - initCursorPosition > 0) {
                    addedChar = charSequence.charAt(finalCursorPosition - 1);
                    Log.d(TAG, "onTextChanged added: " + addedChar);
                   //added char

                } else {
                    char delChar = initialChar.charAt(initCursorPosition - 1);
                   Log.d(TAG, "onTextChanged deletedChar: " + delChar);
                   //deleted char
                }

        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int start, int before, int after) {
            Log.d(TAG, "textChange beforeTextChanged: " + charSequence);
            Log.d(TAG, "textChange cursorPosition: " + editText.getSelectionStart());
            initialChar = String.valueOf(charSequence);
            initCursorPosition = editText.getSelectionStart();
        }

        @Override
        public void afterTextChanged(Editable arg0) {
        }
    });
0
(Edittext).setOnKeyListener(new OnKeyListener()
{
    public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            if(event.getKeyCode()==67)
            {
                if((EditText).getText().toString().length()>0)
                {
                               int pos = (Edittext).getSelectionStart();
                               Char c = (EditText).getText().toString.charAt(pos);
       Toast.makeText(getApplicationontext(),String.valueOf(c),Toast.LENGTH_SHORT).show();
                }
        }   
            return false;
        }
    });

I think it helps you

Sathyajith Bhat
  • 19,739
  • 21
  • 90
  • 126
Ajay
  • 1,219
  • 11
  • 28
-2

Try this

edittext.addTextChangeListener(new TextWatcher{
@override
  public void afterTextChanged(Editable s){
     String changedtext = s.toString();
}

@override
public void  beforeTextChanged (CharSequence s, int start, int count, int after){}

@override
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
Ronak Mehta
  • 5,882
  • 5
  • 40
  • 67
techieWings
  • 2,025
  • 2
  • 15
  • 18
  • The reason of down vote is you have added correct listeners but not completely answered the question e.g where ru u doing deletion or checking deletion . – Naveed Aug 03 '13 at 02:57
  • Answer is just to let person know that, he can use this to achieve his requirements. – techieWings Aug 23 '13 at 13:07