gpt4 book ai didi

android - 如何将单个 TextWatcher 用于多个 EditText?

转载 作者:IT老高 更新时间:2023-10-28 13:00:59 26 4
gpt4 key购买 nike

我的 View 布局中有三个 EditText 小部件。有没有办法为所有三个 EditTexts 使用单个 TextWatcher

最佳答案

我刚遇到这个问题。我通过创建一个以 View 作为参数的 TextWatcher 内部类实现来解决它。然后,在方法实现中,只需打开 View 即可查看 Editable 来自哪一个

声明:

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.name:
model.setName(text);
break;
case R.id.email:
model.setEmail(text);
break;
case R.id.phone:
model.setPhone(text);
break;
}
}
}

用法:

name = (EditText) findViewById(R.id.name);
name.setText(model.getName());
name.addTextChangedListener(new GenericTextWatcher(name));

email = (EditText) findViewById(R.id.email);
email.setText(model.getEmail());
email.addTextChangedListener(new GenericTextWatcher(email));

phone = (EditText) findViewById(R.id.phone);
phone.setText(model.getPhone());
phone.addTextChangedListener(new GenericTextWatcher(phone));

关于android - 如何将单个 TextWatcher 用于多个 EditText?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5702771/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com