【问题标题】:The count parameter in onTextChanged method of TextWatcher is not working properly for EditText with input type textWebPasswordTextWatcher 的 onTextChanged 方法中的 count 参数对于输入类型为 textWebPassword 的 EditText 无法正常工作
【发布时间】: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


【解决方案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));
        }
    }

【讨论】:

  • 我知道这是一个老问题/答案,但 count 是这个值,因为根据 TextWatcher 文档:“调用此方法是为了通知您,在 s 内,从 start 开始的 count 个字符刚刚替换了之前有长度的旧文本。” (developer.android.com/reference/android/text/…)。这不是 S 的长度,如您所料。
【解决方案2】:

我在使用 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

希望对你有帮助!

【讨论】:

  • 但是为什么会这样
  • 可能其中一个拥有基于 vanila android 的自定义操作系统。
猜你喜欢
  • 1970-01-01
  • 2013-07-05
  • 2016-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-08
  • 2021-04-16
相关资源
最近更新 更多