0

I created an Application to dial particular contact number. It has one EditText and ten buttons for the digits from 0 to 9 and a BACK button. I want to erase single digit from EditText on each click event of BACK button. Is there any way to do so ?

Jaimin Modi
  • 1,530
  • 4
  • 20
  • 72
  • yes, there is :) but I would prefer to show you what to add to your own code, so please show us what you've got so far – Bö macht Blau Oct 02 '15 at 06:01
  • I have done same as following solution, and All is going fine. But, What if I want to erase from particular position ie. from the middle. – Jaimin Modi Oct 02 '15 at 06:40

1 Answers1

2

Namaskar modiji, try

myButton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View view){
     String text = et.getText().toString();
     if(!TextUtils.isEmpty(text)){
                String newText = text.substring(1, text.length()); //delete from left
                //or
                String newText1 = text.substring(0, text.length() - 1); //delete from right
                et.setText(newText);
                et.setSelection(newText.length());
                //or
                et.setText(newText1);
                et.setSelection(newText1.length());

     } 
   }
}
subhash
  • 2,689
  • 18
  • 13
  • What about the position of the Cursor ? – Jaimin Modi Oct 02 '15 at 06:27
  • you can get cursor position by myEditText.getSelectionStart(); or myEditText.getSelectionEnd(); http://stackoverflow.com/questions/6900408/get-cursor-position-in-android-in-edit-text now use logic as above – subhash Oct 02 '15 at 06:55
  • This code causes the first character removal but when click again, it brings back the first click's removal. Interesting. – Bay Jan 20 '21 at 15:58