在前端开发中,可以通过以下方法检测哪个UI EditText被激活:
示例代码:
EditText editText1 = findViewById(R.id.editText1);
EditText editText2 = findViewById(R.id.editText2);
editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// editText1 获得焦点
} else {
// editText1 失去焦点
}
}
});
editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// editText2 获得焦点
} else {
// editText2 失去焦点
}
}
});
示例代码:
EditText editText1 = findViewById(R.id.editText1);
EditText editText2 = findViewById(R.id.editText2);
TextWatcher textWatcher1 = 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) {
}
@Override
public void afterTextChanged(Editable s) {
// editText1 文本变化后的处理
}
};
TextWatcher textWatcher2 = 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) {
}
@Override
public void afterTextChanged(Editable s) {
// editText2 文本变化后的处理
}
};
editText1.addTextChangedListener(textWatcher1);
editText2.addTextChangedListener(textWatcher2);
通过以上方法,可以检测到哪个 UI EditText 组件被激活,并进行相应的处理。
领取专属 10元无门槛券
手把手带您无忧上云