当 editText TextWatcher.onTextChanged 上的 setText 未调用时
when setText on editText TextWatcher.onTextChanged not called
每当 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:");
}
怎么称呼它?
尝试以下操作:
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
请确保您在 setText
调用之前使用 TextWatcher
进行订阅。
这是您的代码的修改版本。这个想法是在添加 TextWatcher
之后在 EditText
中设置文本。
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:");
}
由于某些原因,我没有调用方法 addTextChangedListener
。
但是方法 doBeforeTextChanged
、doOnTextChanged
和 doAfterTextChanged
工作得很好 :
amountTextField.doBeforeTextChanged { text, start, count, after ->
Log.d(TAG, "beforeTextChange.") // OK, called
}
amountTextField.doOnTextChanged { text, start, before, count ->
Log.d(TAG, "onTextChanged.") // OK, called
}
amountTextField.doAfterTextChanged {
Log.d(TAG, "afterTextChanged.") // OK, called
}
amountTextField.addTextChangedListener {
object: TextWatcher {
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
Log.d(TAG, "beforeTextChange.") // NOK, not called
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
Log.d(TAG, "onTextChanged.") // NOK, not called
}
override fun afterTextChanged(p0: Editable?) {
Log.d(TAG, "onTextChanged.") // NOK, not called
}
} }
顺便说一句,结果总是一样的:无论我是否为我的 EidtText 设置了任何文本,无论是在我设置任何文本之后还是之前。
每当 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:");
}
怎么称呼它?
尝试以下操作:
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
请确保您在 setText
调用之前使用 TextWatcher
进行订阅。
这是您的代码的修改版本。这个想法是在添加 TextWatcher
之后在 EditText
中设置文本。
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:");
}
由于某些原因,我没有调用方法 addTextChangedListener
。
但是方法 doBeforeTextChanged
、doOnTextChanged
和 doAfterTextChanged
工作得很好 :
amountTextField.doBeforeTextChanged { text, start, count, after ->
Log.d(TAG, "beforeTextChange.") // OK, called
}
amountTextField.doOnTextChanged { text, start, before, count ->
Log.d(TAG, "onTextChanged.") // OK, called
}
amountTextField.doAfterTextChanged {
Log.d(TAG, "afterTextChanged.") // OK, called
}
amountTextField.addTextChangedListener {
object: TextWatcher {
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
Log.d(TAG, "beforeTextChange.") // NOK, not called
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
Log.d(TAG, "onTextChanged.") // NOK, not called
}
override fun afterTextChanged(p0: Editable?) {
Log.d(TAG, "onTextChanged.") // NOK, not called
}
} }
顺便说一句,结果总是一样的:无论我是否为我的 EidtText 设置了任何文本,无论是在我设置任何文本之后还是之前。