EditText editText = findViewById(R.id.edit_text); editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { // Do something when the EditText gains focus } else { // Do something when the EditText loses focus } } });
EditText editText1 = findViewById(R.id.edit_text1); EditText editText2 = findViewById(R.id.edit_text2); editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus) { // Validate input and display error message if necessary } } }); editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus) { // Validate input and display error message if necessary } } });In this example, we have two EditText views with IDs "edit_text1" and "edit_text2". We are setting an OnFocusChangeListener on each EditText that will validate the input when it loses focus. If the input is not valid, an error message will be displayed. The android.widget package is a part of the Android UI toolkit, which provides a set of graphical user interface (GUI) components, such as buttons, text fields, and menus, for building Android apps.