2016-03-03 28 views
2

我在三個不同的過濾器的視圖佈局中使用了三個EditText小部件。如果我輸入其中一個,不應該有另一個EditTexts爲空?如何處理多個EditTexts的單個TextWatcher事件?

下面是我的片段:

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 f蘆葦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

+0

你看過這個嗎? http://stackoverflow.com/questions/4283062/textwatcher-for-more-than-one-edittext –

+0

謝謝#詹姆斯布里頓 – Pitty

回答

1

null您的剩餘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(""); 
     } 
    });