Reputation: 37
I want to make a button that acts like a backspace but I cant find the perfect code for doing it, like I made a button that adds certain characters to a Textview using append() method in a single line of code instead of making a multiple line thingy. So my question is: is there a way to do it in a backspace button code, I know I can make a multiple line/long code, but is there an opposite to append()?
Upvotes: 1
Views: 1697
Reputation: 1291
To remove Character using Backspace Button.
First initialize view
Button BackSpaceButton;
Then Find the View
BackSpaceButton = findViewById(R.id.BackBtn);
Then Add Listener to Button
BackSpaceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String word = EditTextInputNumbers.getText().toString();
int input = word.length();
if (input > 0) {
EditTextInputNumbers.setText(word.substring(0, input - 1));
}
}
});
Upvotes: 0
Reputation: 116
This works for character backspace:
buttonBackspace.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String word = editText.getText().toString();
int input = word.length();
if (input > 0){
editText.setText(word.substring(0, input-1));
}
}
});
Upvotes: 2