每当更改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:");
}
我怎么称呼它?
答案 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) {
}
});