Android: for cycle to handle lost focus on TextEdit fields

107 views Asked by At

I searched for long without success about making this piece of code in one shot by means, for instance, of a for cycle rather than repeating it 10 times (D1 d1, D2 d2, ..., D10 d10):

    //
    // -------------- DIAMETRO 1 -----------------------------------
    //
    final EditText D1 = (EditText)findViewById(R.id.d1);
    int d1 = mast.getInstance().getd1();
    Log.d("Diametro 1 =", Integer.toString(d1));
    D1.setText(Integer.toString(d1));
    //
    // Perdita del focus del diametro 1
    //
    D1.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
        /* When focus is lost check that the text field
        * has valid values.
        */
            if (!hasFocus) {
                String strD1=D1.getText().toString();
                mast.getInstance().setd1(Integer.valueOf(strD1));
            }
        }
    });
1

There are 1 answers

0
Felix Gutierrez On

More detail here!

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
            }
        }
    });