How to get the object with focus on Android

1
I have the following Listener below which checks if an editText has been changed, the Listener will be identical for several editText so I would like to get the current editText through the "v" property, to avoid code repetition and create only one "onFocusChangeListener" and assign to various editText.

        edtDescPer.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            executeOnChange = false;
            v.focus

            if (hasFocus) {
                sTemp = edtDescPer.getText().toString();
                edtDescPer.setText("");
            } else {
                if (edtDescPer.getText().toString().equals(""))
                    edtDescPer.setText(sTemp);
            }

            executeOnChange = true;
        }
    });
    
asked by anonymous 12.09.2016 / 16:33

3 answers

5

You can get the element by getting the view id, using the getId() method, for example

public void onFocusChange(View v, boolean hasFocus) {

 switch (v.getId()) {
    case R.id.editText1:
        //caso for o editText1
        break;
    case R.id.editText2:
        //caso for o editText2
        break;
    }
}

Create%, separated, not already as edittext parameter, for example:

View.OnFocusChangeListener exemplo = new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                 switch (v.getId()) {
                      case R.id.editText1:
                         //caso for o editText1
                         break;
                      case R.id.editText2:
                        //caso for o editText2
                         break;
                      }
            }
        } ;

and then only assign to edittext

editText1.setOnFocusChangeListener(exemplo);
editText2.setOnFocusChangeListener(exemplo);
    
12.09.2016 / 16:44
1

Below is the onFocusChangeListener function statement made with Marco Giovanni's hint:

    View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        executeOnChange = false;
        EditText et = (EditText) findViewById(v.getId());

        if (hasFocus) {
            sTemp = et.getText().toString();
            et.setText("");
        } else {
            if (et.getText().toString().equals(""))
                et.setText(sTemp);
        }

        executeOnChange = true;
    }
} ;

Role assignment in onCreate:

edtDescPer.setOnFocusChangeListener(onFocusChangeListener);
    
12.09.2016 / 19:53
-1

You can have your class implement OnFocusChangeListener , like this:

public class MainActivity extends Activity implements OnFocusChangeListener {
    private EditText editText1, editText2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText1 = (EditText) findViewById(R.id.editText1);
        editText1.setOnFocusChangeListener(this);
        editText2 = (EditText) findViewById(R.id.editText2);
        editText2.setOnFocusChangeListener(this);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        String liganame = editText1.getText().toString();
        String liganame2 = editText2.getText().toString();

        if(liganame.length() == 0) {
            if(editText1.requestFocus()) {
                //primeiro
            }
        }

        if(liganame2.length() == 0) {
            if(editText2.requestFocus()) {
                //segundo
            }
        }

    }
}
    
12.09.2016 / 16:38