संक्षिप्त जवाब
आप यह देख सकते हैं कि किस दृश्य में वर्तमान में उपयोगकर्ता और प्रोग्राम ट्रिगर घटनाओं के बीच अंतर करने के लिए फ़ोकस है।
EditText myEditText = (EditText) findViewById(R.id.myEditText);
myEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (myEditText.hasFocus()) {
// is only executed if the EditText was directly changed by the user
}
}
//...
});
लंबा जवाब
संक्षिप्त उत्तर के अतिरिक्त: यदि आपके myEditTextपास उस प्रोग्राम को बदलने के लिए पहले से ही फोकस है, जिसे आपको कॉल करना चाहिए clearFocus(), तो आप कॉल करते हैं setText(...)और आपके द्वारा फ़ोकस का पुनः अनुरोध करने के बाद। यह एक उपयोगी कार्य में एक अच्छा विचार होगा:
void updateText(EditText editText, String text) {
boolean focussed = editText.hasFocus();
if (focussed) {
editText.clearFocus();
}
editText.setText(text);
if (focussed) {
editText.requestFocus();
}
}
कोटलिन के लिए:
चूंकि कोटलिन विस्तार कार्यों का समर्थन करता है इसलिए आपकी उपयोगिता फ़ंक्शन इस तरह दिख सकती है:
fun EditText.updateText(text: String) {
val focussed = hasFocus()
if (focussed) {
clearFocus()
}
setText(text)
if (focussed) {
requestFocus()
}
}