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

Nis*_*hal 1 android textwatcher android-edittext android-inputtype

TextWatcher的onTextChanged方法中的count参数对于输入类型为textWebPassword的EditText无法正常工作。即使EditText中有超过1个字符,else if(count == 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) {

    }
};
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Nis*_*hal 5

经过研究,我使用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));
        }
    }
Run Code Online (Sandbox Code Playgroud)


MTu*_*ash 5

我在使用onTextChanged方法时遇到了类似的问题!该计数指标全部为1,无论多久,这个词被其插入,和之前指标总是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
...
Run Code Online (Sandbox Code Playgroud)

有趣的是,这发生在一个 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());
    }

}
Run Code Online (Sandbox Code Playgroud)

现在我可以捕捉到 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
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!