我做了一個非常簡單的Android活動:TextWatcher方法和重寫EditText.onTextChanged()不叫一邊跑robolectric測試
public class SimpleActivity extends Activity {
private EditText editText1;
private EditText editText2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout verticalLayout = new LinearLayout(this);
verticalLayout.setOrientation(LinearLayout.VERTICAL);
editText1 = new EditText(this) {
@Override
protected void onTextChanged(CharSequence text, int start,
int lengthBefore, int lengthAfter) {
// TODO Auto-generated method stub
super.onTextChanged(text, start, lengthBefore, lengthAfter);
}
};
editText1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
System.out.println(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
editText1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
verticalLayout.addView(editText1);
editText2 = new EditText(this);
editText2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
verticalLayout.addView(editText2);
setContentView(verticalLayout);
}
public EditText getEditText1() {
return editText1;
}
public EditText getEditText2() {
return editText2;
}
}
當我在我的Android設備上運行此代碼,都被覆蓋的的EditText .onTextChanged()方法&在組件被inited時調用TextWatcher.onTextChanged()方法。此外,他們正在正確調用,當我打電話方法editText1.setText(「文本」)
當我在Roboelectric測試運行創造SimpleActivity,它的創建& editText1/editText2不爲空,但EditText.onTextChanged()& TextWatcher .onTextChanged()在調用EditText.setText()時不調用組件初始值&如何使TextWatcher工作?這是我的應用&的一部分,它不能在沒有處理文本更改事件的情況下進行測試。
請幫助。
非常感謝。不理解,以前的影子類是什麼,現在看Robolectric代碼更貼近和理解。我剛剛使用了老版本的Robolectric,它根本沒有改變事件支持。 – 32kda