我是Android开发的新手,我想创建一个每次更改editText时运行的方法。
在 swift 中我使用此功能:
func fieldChanged(textfieldChange: UITextField){
}
我找不到任何类似Java和Android Studio的功能,有什么建议吗?
答案 0 :(得分:1)
// Globally declare a variable
private TextWatcher mTextWatcher;
// Call textChanger() method in onCreate
private void textChanger() {
mTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() == 6 && mEditText.getText().toString().trim().equals("some value to check"))
mEditTextOther.requestFocus();
else
mEditText.setError("enter correct");
}
@Override
public void afterTextChanged(Editable s) { }
};
mEditText.addTextChangedListener(mTextWatcher);
}
答案 1 :(得分:0)
试试这个
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});