2015-08-19 1 views
0

Le paramètre count de la méthode onTextChanged de TextWatcher ne fonctionne pas correctement pour EditText avec le type d'entrée textWebPassword. Le code à l'intérieur d'else if (count == 1) est en cours d'exécution même s'il y a plus de 1 caractères dans EditText.Le paramètre count de la méthode onTextChanged de TextWatcher ne fonctionne pas correctement pour EditText avec le type d'entrée textWebPassword

public class Test extends AppCompatActivity { 
private EditText ePassword; 
private TextView tPassword; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_sign_in); 
    tPassword = (TextView) findViewById(R.id.textView_password); 
    ePassword = (EditText) findViewById(R.id.editText_password); 
    ePassword.addTextChangedListener(textWatcherPassword); 


} 
private TextWatcher textWatcherPassword = new TextWatcher() { 
    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

    } 

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

     if (count == 0) { 
      // start fade out animation 
      tPassword.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out)); 
      //Make the elements invisible 
      tPassword.setVisibility(View.INVISIBLE); 

     } 
     else if(count==1){ 
      // Make fade in elements Visible first 
      tPassword.setVisibility(View.VISIBLE); 
      // start fade in animation 
      tPassword.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in)); 
     } 
     Log.e("Count", count + ""); 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 

    } 
}; 
} 

sortie Logcat est (pour le mot de passe avec plus de 1 caractère):

08-19 16:54:49.465 25167-25167/com.test.example E/Count﹕ 1 
08-19 16:54:49.607 25167-25167/com.test.example E/Count﹕ 1 
08-19 16:54:49.756 25167-25167/com.test.example E/Count﹕ 1 
08-19 16:54:49.881 25167-25167/com.test.example E/Count﹕ 1 
08-19 16:54:50.006 25167-25167/com.test.example E/Count﹕ 1 
08-19 16:54:50.122 25167-25167/com.test.example E/Count﹕ 1 
+0

Utilisez logcat pour enregistrer exactement ce que vous recevez dans 'count', alors vous serez capable de savoir ce qui se passe vraiment. – Sebastian

+0

Après quelques recherches, j'ai eu mon code en cours d'exécution en utilisant s.length() == 1 au lieu de count == 1. – Nischal

Répondre

3

Après quelques recherches, je me suis mon code en cours d'exécution en utilisant s.length() == 1 au lieu de count = = 1.

@Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     if (s.length() == 0) { 
      // start fade out animation 
      tPassword.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out)); 
      //Make the elements invisible 
      tPassword.setVisibility(View.INVISIBLE); 

     } else if (s.length() == 1) { 
      // Make fade in elements Visible first 
      tPassword.setVisibility(View.VISIBLE); 
      // start fade in animation 
      tPassword.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in)); 
     } 
    } 
3

J'ai eu un problème similaire en utilisant la méthode OnTextChanged! L'indicateur de comptage était toujours 1 quel que soit combien de temps le mot a été qui a été inséré, et le avant que l'indicateur était toujours 0. Vérifiez les journaux:

01-01 03:15:35.440: E/OLE(22401): onTextChangedOLD word: start:0 before:0 count:0 
01-01 03:15:39.380: E/OLE(22401): onTextChangedOLD word:S start:0 before:0 count:1 
01-01 03:15:41.170: E/OLE(22401): onTextChangedOLD word:SE start:1 before:0 count:1 
01-01 03:15:41.170: E/OLE(22401): onTextChangedOLD word:SEK start:2 before:0 count:1 
... 

chose drôle est que ce happend sur un appareil Android (rockchip) tandis que sur un autre (samsung onglet 2) cela a fonctionné correctement. Les deux ont la version Android 4.2.2, tandis que le problème était un dispositif android spécifique de rockchip manufacturor.

Je résolu le problème en utilisant les méthodes beforeTextChanged et afterTextChanged.

public class CustomAutoCompleteTextChangedListener implements TextWatcher { 

Context context; 
int startC, countC, beforeC, afterC; 
CharSequence userInputC; 

public CustomAutoCompleteTextChangedListener(Context context){ 
    this.context = context; 
    this.startC = 0; 
    this.countC = 0; 
    this.beforeC = 0; 
    this.afterC = 0; 
    this.userInputC = ""; 
} 

@Override 
public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

    countC = s.length(); 
    startC = countC; 
    beforeC = countC; 
    afterC = countC; 
} 

@Override 
public void onTextChanged(CharSequence userInput, int start, int before, int count) { 

    userInputC = userInput; 
    afterC = countC++; 
} 

@Override 
public void afterTextChanged(Editable s) { 

    ModActivity modActivity = ((ModActivity) context); 

    //we only search if the user inserts more than 2 chars 
    if(countC==2 && beforeC == 1){ 

     // query the database based on the user input 
     modActivity.startDBSearch(userInputC.toString()); 
    } 

} 

Maintenant je pourrais attraper l'apparition correcte de l'avant et l'indicateur compte. La nouvelle sortie du journal était la suivante:

01-01 03:15:35.440: E/OLE(22401): onTextChanged word: start:0 before:0 count:1 
01-01 03:15:39.380: E/OLE(22401): onTextChanged word:S start:0 before:0 count:1 
01-01 03:15:41.170: E/OLE(22401): onTextChanged word:SE start:1 before:1 count:2 
01-01 03:15:41.170: E/OLE(22401): onTextChanged word:SEK start:2 before:2 count:3 

Espérons que cela aide!

+0

Mais pourquoi cela se produit –