2013-08-29 57 views
6

我有5個addTextChangedListener用於5個編輯文本。我想在一個函數中自定義它們,這樣我就只需傳遞editttext的id,然後addTextChangedListener就可以應用了。我該怎麼辦這我沒有得到這個。請幫助我。在函數中設置addTextChangedListener Android

代碼

mobileNumber.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       mobileNumber.setError(null); 
      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
       // TODO Auto-generated method stub 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       mobileNumber.setError(null); 
      } 
     }); 

     mobileNumber2.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       mobileNumber2.setError(null); 
      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
       // TODO Auto-generated method stub 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       mobileNumber2.setError(null); 
      } 
     }); 

     mobileNumber1.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       mobileNumber1.setError(null); 
      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
       // TODO Auto-generated method stub 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       mobileNumber1.setError(null); 
      } 
     }); 

所有addTextChangedListener正在爲不同的ID同樣的事情。所以這是可能在android系統定製它的功能,我們必須只是傳遞上並addTextChangedListener的ID將上

作爲應用建議

public class BookingClassAddListenerOnTextChange implements TextWatcher { 
    private Context mContext; 
    EditText mEdittextview; 

    public BookingClassAddListenerOnTextChange(Context context, EditText edittextview) { 
     super(); 
     this.mContext = context; 
     this.mEdittextview= edittextview; 

    } 
    @Override 
    public void afterTextChanged(Editable arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
      int arg3) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
     // TODO Auto-generated method stub 

    } 
    public void addTextChangedListener(
      BookingClassAddListenerOnTextChange bookingClassAddListenerOnTextChange) { 
     // TODO Auto-generated method stub 

    } 

} 

回答

10

您應該像這樣創建一個Listener類,只需修改構造函數 中的參數即可接受要添加偵聽器的EditText ID。

public class addListenerOnTextChange implements TextWatcher { 
    private Context mContext; 
    EditTextView mEdittextview; 

    public addListenerOnTextChange(Context context, EditTextView edittextview) { 
     super(); 
     this.mContext = context; 
     this.mEdittextview= edittextview; 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
    } 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     //What you want to do 
     } 
    } 
} 

你在你的活動叫他們這樣需要

mobileNumber2.addTextChangedListener(new addListenerOnTextChange(this, mobileNumber2)); 

再次修改參數。

+0

設置我沒有得到我怎麼會從我的更新現在我怎麼會從我的活動類調用這個和我的代碼活動 – Developer

+0

調用此什麼是適配器 – Developer

+0

@Gaurav,對不起,更新我的答案並刪除了適配器。那是爲了我自己的代碼。您將像這樣調用它,'mobileNumber2 =(EditText)findViewById(R.id.text)和mobileNumber2.addTextChangedListener(new addListenerOnTextChange(this,mobileNumber2);' – linus

1

如果所有五個行爲以相同的方式,那麼你可以創建一個TextWatcher對象並將其傳遞給所有對象。

TextWatcher tw = new TextWatcher() { 
    /* code goes here */ 
    /* viewOnFocus can be used here */ 
} 

mobilenum1.addTextChangedListener(tw); 
mobilenum2.addTextChangedListener(tw); 
mobilenum3.addTextChangedListener(tw); 

viewOnFocus變量通過一個onFocusChange聽衆

​​