How to add a global focus listener for 30 EditText in android

373 Views Asked by At

I'm trying to make a crossword on android and I want to add a focuslistener for all the EditText so when I write a character on it and change my focus to other EditText it will check if I enter the right character (set background green) or not (set background red).

I have this code:

campo0 = new CampoCrossword(R.id.et_2_8_O, 'O');
campo1 = new CampoCrossword(R.id.et_2_7_E, 'E');
campo2 = new CampoCrossword(R.id.et_2_6_M, 'M');
campo3 = new CampoCrossword(R.id.et_2_5_R, 'R');
campo4 = new CampoCrossword(R.id.et_4_7_A, 'A');
campo5 = new CampoCrossword(R.id.et_4_8_T, 'T');
campo6 = new CampoCrossword(R.id.et_4_9_A, 'A');
campo7 = new CampoCrossword(R.id.et_4_4_R, 'R');
campo8 = new CampoCrossword(R.id.et_4_5_E, 'E');
campo9 = new CampoCrossword(R.id.et_2_4_E, 'E');
campo10 = new CampoCrossword(R.id.et_2_3_B, 'B');
campo11 = new CampoCrossword(R.id.et_3_6_A, 'A');
campo12 = new CampoCrossword(R.id.et_4_6_G, 'G');
campo13 = new CampoCrossword(R.id.et_5_6_D, 'D');
campo14 = new CampoCrossword(R.id.et_6_7_K, 'K');
campo15 = new CampoCrossword(R.id.et_6_8_A, 'A');
campo16 = new CampoCrossword(R.id.et_6_5_D, 'D');
campo17 = new CampoCrossword(R.id.et_6_3_U, 'U');
campo18 = new CampoCrossword(R.id.et_5_3_J, 'J');
campo19 = new CampoCrossword(R.id.et_7_3_L, 'L');
campo20 = new CampoCrossword(R.id.et_10_4_I, 'I');
campo21 = new CampoCrossword(R.id.et_10_5_Z, 'Z');
campo22 = new CampoCrossword(R.id.et_10_7_R, 'R');
campo23 = new CampoCrossword(R.id.et_10_8_O, 'O');
campo24 = new CampoCrossword(R.id.et_9_3_O, 'O');
campo25 = new CampoCrossword(R.id.et_8_3_I, 'I');
campo26 = new CampoCrossword(R.id.et_6_2_M, 'M');
campo27 = new CampoCrossword(R.id.et_6_4_N, 'N');
campo28 = new CampoCrossword(R.id.et_6_6_A, 'A');
campo29 = new CampoCrossword(R.id.et_8_6_E, 'E');
campo30 = new CampoCrossword(R.id.et_9_6_N, 'N');
campo31 = new CampoCrossword(R.id.et_10_6_A, 'A');
campo32 = new CampoCrossword(R.id.et_7_6_L, 'L');

/* I was trying to do it with one first and then with all but it doesn't work xD*/
findViewById(R.id.et_2_8_O).setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            check();
        }
    });
2

There are 2 best solutions below

0
On

You've created a bunch of Views (I assume that CampoCrossword is a class that extends View, but when you call findViewById(), the framework looks in your current View hierarchy (as set by setContentView()) and returns a View with the specified ID. Just creating all those instances of CampoCrossword doesn't actually put them in the View hierarchy.

0
On

I solved my problem, i entered all the edittext on arraylist and use a foreach to add focuschangelistener, like this

 for (CampoCrossword campo:ListaCampos) {
        viewGeneralFragmen.findViewById(campo.id).setOnFocusChangeListener(new View.OnFocusChangeListener(){
            @Override
            public void onFocusChange(View v, boolean hasFocus){
                comprobarCampos();
                aplicarColor();
                comprobarSiHasGanado();
            }
        });

btw ListaCampos is the arraylist, CampoCrosword is the class where i instanciate one object wit EditText id