Reputation:
Long story short I am facing this problem: I am attaching a textwatcher to an edittext. As soon as "1" is the last char written on it, that should be replaced with the letter "a". But here is the problem: I'd like as soon as "a" is also the last char written on the edittext (user pressed "a"), EXCEPT THROUGH THE PREVIOUS METHOD, some things to be done. But as I test it and type "1", that is converted to "a" normally and the things I mention are also done. I can't seem to find a way to overpass this, can any suggestions be given? Thanks a lot. I use:
public void afterTextChanged(Editable s) {
//TODO Auto-generated method stub
if (s.length() > 0 && s.toString().charAt(s.length() - 1) == '1') {
current_string = s.toString().substring(0, (s.length() - 1));
et.setText(current_string + "a");
length = s.length();
et.setSelection(length);
}
else if (s.length() > 0 && s.toString().charAt(s.length() - 1) == 'a') {
//do some things
}
Upvotes: 0
Views: 116
Reputation: 362
Do you mean.
1)when user enters the 1 in end it should replace by 'a'. 2)when user enters 'a' separately (not converted by the step 1) it should do something else
amount1.addTextChangedListener(new TextWatcher() {
int flagg;
public void onTextChanged(CharSequence s, int start, int before,
int count) {
System.out.println("flagg"+s+"#"+flagg );
if (s.length() > 0 && s.toString().charAt(s.length() - 1) == '1' )
{ System.out.println("Converting and doing NOthing");
flagg=1;
String current_string = s.toString().substring(0,(s.length() - 1));
amount1.setText(current_string + "a");
int length = s.length();
amount1.setSelection(length);
}
else if (s.length() > 0 && ( s.toString().charAt(s.length() - 1) == 'a') && flagg == 0 ) {
System.out.println("Staying same and doing Something");
amount2.setText(amount2.getText().toString() + "Z");
// do some things
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if (start != 0) {
if ((s.toString()).substring(s.length() - 1).equals("a")) {
flagg = 1;
}
if ((s.toString()).substring(s.length() - 1).equals("1")) {
flagg = 0;
} else {
flagg = 0;
}
}
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
//new flagg is set in if condition
Upvotes: 0
Reputation: 470
try this
public void afterTextChanged(Editable s) {
if (s.length() > 0 && s.toString().charAt(s.length() - 1) == 'a') {
//do some things
}
if (s.length() > 0 && s.toString().charAt(s.length() - 1) == '1') {
current_string = s.toString().substring(0, (s.length() - 1));
et.setText(current_string + "a");
length = s.length();
et.setSelection(length);
}
Upvotes: 1