对于输入类型为 textWebPassword 的 EditText,TextWatcher 的 onTextChanged 方法中的计数参数无法正常工作。 即使 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) {
}
};
}
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));
}
}
关于android - 对于输入类型为 textWebPassword 的 EditText,TextWatcher 的 onTextChanged 方法中的计数参数无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32093575/