每当更改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:");
}

我怎么称呼它?

最佳答案

因此,这是您的代码的修改后的版本。这个想法是在您添加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:");
}

关于java - 当未调用editText TextWatcher.onTextChanged上的setText时,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39190058/

10-09 04:50