Skąd mogę wiedzieć, kiedy EditText traci ostrość?

Muszę złapać, gdy EditText traci ostrość, Szukałem innych pytań, ale nie znalazłem odpowiedzi.

Użyłem OnFocusChangeListener w ten sposób

OnFocusChangeListener foco = new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // TODO Auto-generated method stub

    }
};
Ale to nie działa na mnie.
Author: dkmann, 2012-05-17

3 answers

Zaimplementuj onFocusChange z setOnFocusChangeListener i jest parametr logiczny dla hasFocus. Gdy jest to fałszywe, straciłeś koncentrację na innej kontroli.

 EditText txtEdit = (EditText) findViewById(R.id.edittxt);

 txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {          
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
               // code to execute when EditText loses focus
            }
        }
    });
 288
Author: ρяσѕρєя K,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-30 06:14:47

Niech twój Activity zaimplementuje OnFocusChangeListener() jeśli chcesz mieć możliwość korzystania z tego interfejsu, przykład:

public class Shops extends AppCompatActivity implements View.OnFocusChangeListener{

W twoim OnCreate możesz dodać listener na przykład:

editTextResearch.setOnFocusChangeListener(this);
editTextMyWords.setOnFocusChangeListener(this);
editTextPhone.setOnFocusChangeListener(this);

Następnie android studio poprosi Cię o dodanie metody z interfejsu, zaakceptuj ją... będzie tak:

@Override
public void onFocusChange(View v, boolean hasFocus) {
// todo your code here...
}

A skoro masz kod faktorowany, to musisz to zrobić:

@Override
public void onFocusChange(View v, boolean hasFocus) {
   if (hasFocus) {
        editTextResearch.setText("");
        editTextMyWords.setText("");
        editTextPhone.setText("");
    }
    if (!hasFocus){
        editTextResearch.setText("BlaBlaBla");
        editTextMyWords.setText(" One Two Tree!");
        editTextPhone.setText("\"your phone here:\"");
    }
}

Wszystko, co zakodujesz w {[7] } jest dla zachowania przedmiotu, który traci ostrość, to powinno załatwić sprawę! Ale uważaj, że w takim stanie zmiana ostrości może nadpisać wpisy użytkownika!

 7
Author: Cerberus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-13 14:26:11

Its Working Property

EditText et_mobile= (EditText) findViewById(R.id.edittxt);

 et_mobile.setOnFocusChangeListener(new OnFocusChangeListener() {          
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
               // code to execute when EditText loses focus
 if (et_mobile.getText().toString().trim().length() == 0) {
                        CommonMethod.showAlert("Please enter name", FeedbackSubmtActivity.this);
                    }
            }
        }
    });



public static void showAlert(String message, Activity context) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage(message).setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                }
            });
    try {
        builder.show();
    } catch (Exception e) {
        e.printStackTrace();
    }

}
 0
Author: Keshav Gera,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-30 12:40:10