This is my present code:
txt.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
{
// TODO: Implement this method
}
@Override
public void onTextChanged(CharSequence p1, int p2, int p3, int p4)
{
Mymethod1; //displays the text entered into a TextView
Mymethod2; //a complicated method which inserts some strings, moves the text-cursor if some conditions are satisfied(conditions also depend on the previous text entered
}
@Override
public void afterTextChanged(Editable p1)
{
// TODO: Implement this method
}
});
If I describe Mymethod1, it simply gets the input text, changes it a bit and displays it on a TextView
Whereas Mymethod2, inserts some strings in the edittext and changes the postion of text cursor using conditions, for example:
RandomText_{}MoreRandomText_<cursor is here>
is changed to RandomText_{}MoreRandomText_{cursor is here>}
so as you see, if an underscore is not followed by a curly bracket, it inserts them and places the cursor inside it.
Also, i made my methods argument independent from the p1,p2,p3,p4 parameters.
I am able to implement this correctly and it also works flawlessly, but a problem occurs when we use Backspace to delete the text in the edit text, in the process of deleting, when the _{}
comes, after {}
getting deleted my method2 starts getting applied. and since the cursor is still placed inside the {}
, it generates something like this TextNotBeingDeleted_{<cursor>}}}}}}}}}}}}}
more backspaces--> more }.
So i decided to use setOnKeyListener like this:
txt.setOnKeyListener(new OnKeyListener(){
@Override
public boolean onKey(View p1, int p2, KeyEvent p3)
{
if(keyCode == KeyEvent.KEYCODE_DEL){
Mymethod1;
}
else{
Mymethod1;
Mymethod2;
}
return false;
}
});
but this isn't working properly, only Mymethod1 gets executed when backspace is used, nothing happens when other keys are used(works like method1,2 doesnt exist). Please guide me on how to solve this problem, if i should continue using addTextChangedListener and change it appropriately(and how)or if I should correct my setOnKetListener(and how).
Thanks