Pergunta

In my app, I have an edittext which will contain a user (brought from db). There is the possibility that more than a user be present inside the edittext in the following manner : UserX, UserY, UserZ

I want to make the backspace of the virtual keyboard function in such a way that upon click, it deletes the whole user, rather than each character.

Any hints how to do this? As I did not find anything upon my searches. (perhaps I was using wrong keywords)

Foi útil?

Solução

I suggest you use a SpannableString.Here is a complete guide : link. Good luck !

Outras dicas

You need to look into the listeners for editing text.

Then, when the user presses backspace, delete till you find a space by maybe using a lastIndexOf() method.

So, you getText() from EditText, you substring() it till the last space character, take that substring and set it as the new text

In this line of code you can do whatever you want when the backspace button is pressed. Something like editText.setText("");

editText.setOnKeyListener(new OnKeyListener() {                 
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        //You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_
         if(keyCode == KeyEvent.KEYCODE_DEL){  
             //this is for backspace
             }
    return false;       
        }
});
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top