Search code examples
androidtextwatcher

How to handle Single TextWatcher event for multiple EditTexts?


I used three EditText widgets in my view layout for three diffrent filters. If I type in one of them, shouldn't another EditTexts be blank?

Below is my fragment:

public class Fragment_Assigned extends Fragment {
    public EditText et_first;
    public EditText et_second;
    public EditText et_third;

    private ArrayList<obj> list_first;
    private ArrayList<obj> list_second;
    private ArrayList<obj> list_third;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        et_first = (EditText) v.findViewById(R.id.et_first);
        et_second = (EditText) v.findViewById(R.id.et_second);
        et_third = (EditText) v.findViewById(R.id.et_third);

        listoffline = //getFrom DataBase
        filterListCustomer = listoffline;
        filterListModel = listoffline;
        filterListCompany = listoffline;

        et_first.addTextChangedListener(new GenericTextWatcher(et_first));
        et_second.addTextChangedListener(new GenericTextWatcher(et_second));
        et_third.addTextChangedListener(new GenericTextWatcher(et_third));
    }
}

GenericTextWatcher method:

private class GenericTextWatcher implements TextWatcher {
    private View view;

    private GenericTextWatcher(View view) {
        this.view = view;
    }

    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    public void afterTextChanged(Editable editable) {
        String text = editable.toString();
        switch (view.getId()) {
            case R.id.et_first:
                //someMethod;
                break;
            case R.id.et_second:
                //someMethod;
                break;
            case R.id.et_third:
                //someMethod;
                break;
        }
    }
}

When I run this and type in EditText then logcat looks like this somehow:

03-03 15:25:39.616 25952-25952/com.xyz.abc I/art: Explicit concurrent mark sweep GC freed 23671(1194KB) AllocSpace objects, 3(43KB) LOS objects, 26% free, 11MB/15MB, paused 908us total 15.894ms

03-03 15:25:39.991 25952-25952/com.xyz.abc I/art: Explicit concurrent mark sweep GC freed 20553(963KB) AllocSpace objects, 2(6MB) LOS objects, 39% free, 4MB/8MB, paused 1.523ms total 22.856ms

03-03 15:25:40.356 25952-25952/com.xyz.abc I/art: Explicit concurrent mark sweep GC freed 14366(568KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 5MB/8MB, paused 2.214ms total 30.546ms


Solution

  • null your remaining editText on focus change

        et_first.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                et_second.setText("");
                et_third.setText("");
            }
        });
    
        et_second.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                et_first.setText("");
                et_third.setText("");
            }
        });
    
        et_third.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                et_second.setText("");
                et_first.setText("");
            }
        });