TextWatcher
工作正常,直到我的活动被破坏并从旧的保存包恢复之前。但是当我从保存的包中恢复时,我onTextChanged()
的两个 EditTexts 都会调用它。即使我不恢复它们的值(不要调用 setText())也会发生这种情况。请注意,用户没有以任何方式进行交互。
代码 :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_trip);
Log.d(TAG, "onCreate");
fromLocation = (EditText) findViewById(R.id.from_location);
toLocation = (EditText) findViewById(R.id.to_location);
// Set EditTexts listeners
setFromLocationEditTextListeres();
setToLocationEditTextListeres();
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
Log.d(TAG, "Restoring state from saved bundle");
...
} else {
Log.e(TAG, "Setting default values");
...
}
}
// Set listener for fromLocation EditText
private void setFromLocationEditTextListeres() {
fromLocation.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Log.d(TAG, "fromLocation gained focus.");
}
}
});
fromLocation.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
Log.e(TAG, "Fromlocation after.");
}
@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 (fromLocation == null) {
Log.d(TAG, "fromLocation is null................");
}
if (count != before) {
editTextValueChanged(fromLocation);
}
}
});
fromLocation.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If user pressed enter
if (keyCode == KeyEvent.KEYCODE_ENTER) {
}
// If pressed other than enter key then let others handle the
// event
return false;
}
});
}
// Set listener for toLocation EditText
private void setToLocationEditTextListeres() {
toLocation.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Log.d(TAG, "toLocation gained focus.");
}
}
});
toLocation.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
Log.e(TAG, "Tolocation after.");
}
@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 != before) {
editTextValueChanged(toLocation);
}
}
});
toLocation.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If user pressed enter
if (keyCode == KeyEvent.KEYCODE_ENTER) {
}
// If pressed other than enter key then let others handle the
// event
return false;
}
});
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
Log.e(TAG, "onSaveInstanceState called.");
// Save activity state
savedInstanceState.putCharSequence(FROM_LOCATION_TEXT,
fromLocation.getText());
savedInstanceState.putCharSequence(TO_LOCATION_TEXT,
toLocation.getText());
super.onSaveInstanceState(savedInstanceState);
}