Вот мое решение
Написать пользовательский SyncEditText
:
public class SyncEditText extends AppCompatEditText implements TextWatcher {
private SyncEditText[] mDependencies;
private boolean shouldSync = true;
public SyncEditText(Context context) {
super(context);
}
public SyncEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SyncEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// This is to avoid text changed event is called multiple time per character because auto suggestion
setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
addTextChangedListener(this);
}
public void setDependencies(SyncEditText... dependencies) {
mDependencies = dependencies;
}
public void setText(CharSequence text, boolean syncDependencies) {
shouldSync = syncDependencies;
setText(text);
Log.d("Log", "Text sync: " + text);
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
@Override
public void afterTextChanged(Editable editable) { }
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (mDependencies == null)
return;
if (!shouldSync) {
// If this text is sync from other SyncEditText, ignore the change
shouldSync = true;
return;
}
Log.d("Log", "Text input: " + charSequence);
// Sync to all dependencies
for (SyncEditText syncEditText : mDependencies) {
syncEditText.setText(charSequence, false);
}
}
}
Использование
<com.example.tamhuynh.testfragment.SyncEditText
android:id="@+id/txt_1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.example.tamhuynh.testfragment.SyncEditText
android:id="@+id/txt_2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.example.tamhuynh.testfragment.SyncEditText
android:id="@+id/txt_3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Установить зависимости в коде:
SyncEditText editText1 = findViewById(R.id.txt_1);
SyncEditText editText2 = findViewById(R.id.txt_2);
SyncEditText editText3 = findViewById(R.id.txt_3);
editText1.setDependencies(editText2, editText3);
editText2.setDependencies(editText1, editText3);
editText3.setDependencies(editText1, editText2);
Все SyncEditText
теперь запускают событие для всех его зависимостей, добавляется дополнительный флаг, чтобы убедиться, что текст не изменен.