0

Can anybody help me to solve this problem.? I have added Dash(-) between phone numbers. Here Below I have mentioned my code.

// Removing from here

Now I want to remove dash(-) when I press back button. In my case I can't able to do that. Let's say If I have insert 3 character, then Dash will automatically add after that three numbers, then if I press back button I am not able to delete dash and even that three number which are before dash.

I had to select all of the text and then I would have to press delete then and then It's going to delete.. But with backspace I Can't delete that.. So how can I do that. Can anybody give me code answer of this..??

Thanks in advance, JT.


Updating from here

public class MainActivity extends Activity {
        EditText inputPhone;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.create_new_member);
            inputPhone = (EditText) findViewById(R.id.phoneText);
            inputPhone.addTextChangedListener(new TextWatcher() {
                int len = 0;
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub
                    String str = inputPhone.getText().toString();
                    if ((str.length() == 3 && len < str.length()) || (str.length() == 7 && len < str.length())) {
                        inputPhone.append("-");
                    }
                }
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    String str = inputPhone.getText().toString();
                    len = str.length();
                }
                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub
                }
            });
        }
    }
jt.
  • 283
  • 2
  • 5
  • 16

1 Answers1

1

follow this method your problem would be solved then

http://www.shaikhhamadali.blogspot.com/2013/06/how-to-apply-niccustom-format-to-edit.html

like this solution,you should also chech the length of you text on backspace.

mark this as answer for others help.

    //get the reference of this edit text field
     EditText  etNICNO_Sender=(EditText)findViewById(R.id.etNICNO_Sender);
        /*add textChangeListner with TextWatcher argument
            by adding text change listner with text watcher we can get three methods of
            Edit Text 1) onTextChanged 2) beforeTextChanged 3) afterTextChanged
            these methods work when user types in text feild.
         */
 etNICNO_Sender.addTextChangedListener(new TextWatcher() {
   int len=0;
   @Override
   public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

  String str = etNICNO_Sender.getText().toString();

  if((str.length()==3 && len <str.length()) || (str.length()==7 && len <str.length())){
                  //checking length  for backspace.
                  etNICNO_Sender.append("-");
                  //Toast.makeText(getBaseContext(), "add minus", Toast.LENGTH_SHORT).show();
                 }
   }
   @Override
   public void beforeTextChanged(CharSequence s, int start, int count,
     int after) {
    // TODO Auto-generated method stub
    String str = etNICNO_Sender.getText().toString();
                         len = str.length();
        }
   @Override
   public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

   }
  });
Hamad
  • 5,096
  • 13
  • 37
  • 65
  • that's what the same as my code, with backspace I cant delete the dash still, with your code also. Thanks for help, but required solution of remove dash with backspace.. – jt. Sep 18 '13 at 11:39
  • i have changed code according to your need, try this now! @jt. – Hamad Sep 18 '13 at 11:53
  • Hello Hamad, May be you didnt get my question.. I want to delete DASH also with backspace, when I delete number, DASH also must be deleted with backspace when I press on backspace, and that I cant delete with it.. there must be required to remove DASH with numbers on backspace.. – jt. Sep 18 '13 at 12:04
  • i understood the question,i solved my problem with this code by also deleting the dashes in format,don't know why your problem is not solving.Are you commiting any other mistake in your code? – Hamad Sep 18 '13 at 12:10
  • Hello Sir, I have updated my code.. you can check it out.. And suggest me If I have made any mistakes in it.. Dont know why its not working in my phone device or emulator.. – jt. Sep 18 '13 at 12:39
  • i have just copy pasted you code and created demo project,it has worked perfect!! – Hamad Sep 18 '13 at 12:50
  • have you cleaned your project and run it again? @jt. – Hamad Sep 18 '13 at 12:51
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37597/discussion-between-jt-and-hamad) – jt. Sep 18 '13 at 13:12