The onClick
method in the onClickListener
of the backspace is event driven, i.e., when the backspace button will be clicked, the onClick
method will be called. So setting test
to true
there won't matter as the for
loop would have already stopped executing by that time, since its outside somewhere.
You can directly use sbletter
to delete its last character like following:
backspace.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sbletter = sbletter.deleteCharAt(sbletter.length - 1);
// Now the last character from sbletter is deleted.
// Use sbletter for whatever purpose you like now.
}
}
Note that deleteCharAt method of StringBuilder and StringBuffer return StringBuilder and StringBuffer objects respectively which carry with them the changed value, that's why I wrote:
sbletter = sbletter.deleteCharAt(sbletter.length - 1); // Will work as expected.
and not just:
sbletter.deleteCharAt(sbletter.length - 1); // Won't work as expected.
Hope that helps.