【发布时间】:2015-08-19 10:57:42
【问题描述】:
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) {
}
};
}
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
【问题讨论】:
-
使用 logcat 准确记录您在
count中收到的内容,然后您将能够知道真正发生了什么。 -
经过一番研究,我使用 s.length()==1 而不是 count==1 来运行我的代码。
标签: android android-edittext textwatcher android-inputtype