对于输入类型为 textWebPassword 的 EditText,TextWatcher 的 onTextChanged 方法中的计数参数无法正常工作

The count parameter in onTextChanged method of TextWatcher is not working properly for EditText with input type textWebPassword

对于输入类型为 textWebPassword 的 EditText,TextWatcher 的 onTextChanged 方法中的计数参数无法正常工作。 else if(count==1) 中的代码是 运行 即使 EditText 中有超过 1 个字符。

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) {

    }
};
}

Logcat 输出为(对于超过 1 个字符的密码):

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

经过一些研究,我通过使用 s.length()==1 而不是 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));
        }
    }

我在使用 onTextChanged 方法时遇到了类似的问题! count 指示符始终为 1,无论插入的单词有多长,而 before 指示符始终为 0。查看日志:

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
...

有趣的是,这发生在一台 android 设备 (rockchip) 上,而在另一台设备 (samsung tab 2) 上它工作正常。两者都有 android 版本 4.2.2,而有问题的是制造商 rockchip 的特定 android 设备。

我使用方法 beforeTextChangedafterTextChanged.

解决了这个问题
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());
    }

}

现在我可以捕捉到 before 和 count 指示器的正确出现。新的日志输出如下:

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

希望对您有所帮助!