如何处理多个EditTexts的单个TextWatcher事件?

时间:2016-03-03 10:24:13

标签: android textwatcher

我在视图布局中使用了三个EditText小部件,用于三个不同的过滤器。如果我输入其中一个,那么另一个EditTexts不应该是空白的吗?

以下是我的片段:

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 方法:

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;
        }
    }
}

当我运行它并输入EditText时,logcat会以某种方式显示:

  

03-03 15:25:39.616 25952-25952 / com.xyz.abc I / art:显式并发标记扫描GC释放23671(1194KB)AllocSpace对象,3(43KB)LOS对象,26%免费,11MB / 15MB,暂停908us总计15.894ms

     

03-03 15:25:39.991 25952-25952 / com.xyz.abc I / art:显式并发标记扫描GC释放20553(963KB)AllocSpace对象,2(6MB)LOS对象,39%免费,4MB / 8MB,暂停1.523ms总计22.856ms

     

03-03 15:25:40.356 25952-25952 / com.xyz.abc I / art:显式并发标记扫描GC释放14366(568KB)AllocSpace对象,0(0B)LOS对象,40%免费,5MB / 8MB,暂停2.214ms总计30.546ms

1 个答案:

答案 0 :(得分:1)

在焦点更改时将剩余的editText设为空

    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("");
        }
    });