如何处理多个 EditText 的单个 TextWatcher 事件?
How to handle Single TextWatcher event for multiple EditTexts?
我在我的视图布局中为三个不同的过滤器使用了三个 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: 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
在焦点更改时清空剩余的 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("");
}
});
我在我的视图布局中为三个不同的过滤器使用了三个 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: 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
在焦点更改时清空剩余的 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("");
}
});