当editText上的textText没有调用TextWatcher.onTextChanged时

时间:2016-08-28 09:56:00

标签: java android inner-classes android-textwatcher

每当更改EditText字符串时,都会调用onTextChanged

现在,当我开始新的Activity并通过Bundle发送数据时,onTextChanged未被调用。

if( getIntent().getExtras() != null) {
    Bundle b = getIntent().getExtras();
    int value = -1;

    if(b != null)
        value = b.getInt("key");
    edit1.setText("Mywords:");
}

我怎么称呼它?

3 个答案:

答案 0 :(得分:2)

确保在TextWatcher来电之前订阅setText

答案 1 :(得分:2)

所以这是代码的修改版本。我们的想法是在您添加EditText后在TextWatcher中设置文字。

if( getIntent().getExtras() != null) {
    Bundle b = getIntent().getExtras();
    int value = -1;

    if(b != null)
        value = b.getInt("key");

    // Add  the TextWatcher here
    edit1.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // Toast.makeText(MainActivity.this, "before text changed", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // Toast.makeText(MainActivity.this, "on text changed", Toast.LENGTH_LONG).show();
        }

        @Override
        public void afterTextChanged(Editable s) {
            // Toast.makeText(MainActivity.this, "after text changed", Toast.LENGTH_LONG).show();
        }
    });

    // Now the set the value in your EditText
    edit1.setText("Mywords:");
}

答案 2 :(得分:0)

尝试以下方法:

edit1.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {}

        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start,
                                  int before, int count) {
        }
    });

参考:android on Text Change Listener