【发布时间】:2015-04-08 15:13:53
【问题描述】:
我有 2 个编辑文本字段,并为它们创建了 2 个单独的侦听器,如下所示。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email=(EditText)findViewById(R.id.email);
password=(EditText)findViewById(R.id.password);
email.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
emailFlag=true;
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
password.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
passwordFlag=true;
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
}
现在单击按钮我只想检查更改了哪个文本字段。
if(emailFlag){
System.out.println("Email is changed");
EditText emailField = (EditText) findViewById(R.id.email);
userEmailId = emailField.getText().toString();
}
if(passwordFlag){
System.out.println("Password is changed");
EditText passwordField = (EditText) findViewById(R.id.password);
userPassword = passwordField.getText().toString();
}
所以我为每个人设置了单独的标志。
但是在按钮单击时,尽管我已经更改/单击了单个字段,但两个字段的标志值都为真。
任何人都可以帮助我解决这个问题。问题是因为我在 oncreate() 方法中添加了侦听器吗?
【问题讨论】:
-
您是否在代码中的 editTexts 中设置了文本?如果你这样做,你会触发监听器和 afterTextChanged()。
标签: android android-edittext changelistener