-1

I have a group of edittext, the number of the edittext depend on how many steps user will type in.

What I want to implement is to show one edittext at initial, then when user type in the current edittext, it will add a new blank edittext below. How should I implement this listener?

I think about using textwatcher, it will listen on every character it changed, but I only need to listen to the first character.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Pineapple
  • 111
  • 1
  • 4

2 Answers2

0

How about an EditText dynamically if the charSequence lenght correspond to your value ?

There you would use a textWatcher on your first Edit

public void afterTextChanged(Editable s) {

}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {
     if ((after == 1) && (count== 0) && start == 0))
        //Implement a new EditText dynamically

}
public void onTextChanged(CharSequence s, int start, int before, int count) {               

}

As explained here ; Differences between TextWatcher 's onTextChanged, beforeTextChanged and afterTextChanged

start is the start index of the text

count is the length of the text before modification

after is the length of the modified text

Marech
  • 157
  • 12
  • Then when I delete the text in edittext, when there is only one char left, it will add a new EditText again. – Pineapple Feb 27 '18 at 01:33
  • Then use the beforeTextChanged function the int after is the futur lenght of the string whereas start is the previous lenght. See my edit – Marech Feb 27 '18 at 13:06
0
editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
     if(s.length == 0){
       // This means user has erased all the text from the edittext.
       // For this instance you have to remove the added edittext.
     }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      if(s.length == 0){
        // It means your this was your initial input before changing text
        // add another edit text at this moment.
      }

    }

    @Override
    public void afterTextChanged(Editable s) {

        // TODO Auto-generated method stub
    }
});