How to implement, I have 8 Edittext, I need to independently in what edittext I will write a number, it should appear immediately in all the other Edittext
1 answer
The essence of the solution is to hang focus focus listeners on each EditText . If the element is in focus, we put a textWatcher on it that modifies all the other EditText . Focus out - remove textWatcher .
Here is the solution for the four elements. For eight - the same.
EditText editText1 = (EditText) findViewById(R.id.edit_text_1); EditText editText2 = (EditText) findViewById(R.id.edit_text_2); EditText editText3 = (EditText) findViewById(R.id.edit_text_3); EditText editText4 = (EditText) findViewById(R.id.edit_text_4); final EditText [] editTexts = new EditText[] {editText1, editText2, editText3, editText4}; final TextWatcher textWatcher = new TextWatcher() { public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { for (EditText editText : editTexts) { if (!editText.hasFocus()) { editText.setText(s); } } } }; for (EditText editText : editTexts) { editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean b) { EditText chosenEditText = (EditText)view; if (b) { chosenEditText.addTextChangedListener(textWatcher); } else { chosenEditText.removeTextChangedListener(textWatcher); } } }); } - You can change the code so that for example I entered a number in the first edittext, and in 2 edittext this number is divided by 2 - fcbarcafc
|
TextWatcher- YuriySPb ♦