How to get the object focused on Android

Asked

Viewed 506 times

1

Hello to all I have the following Listener below which checks if an editText has been changed, the Listener will be identical to several editText so I would like to get the current editText through the "v" property, to avoid code repetition and create only one "onFocushangeCListener" and assign to multiple 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;
        }
    });

3 answers

5


You can get the element by obtaining the view id, through the method getId() , 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 the OnFocusChangeListener, separate, no longer 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);
  • Mark your suggestion was instrumental in solving my problem. I’m going to put the code of how my role turned out in an answer, but I’m going to mark yours as correct

1

Below is the declaration of the function onFocusChangeListener made with Marco Giovanni’s tip:

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

Assignment of function in onCreate:

edtDescPer.setOnFocusChangeListener(onFocusChangeListener);
  • You don’t need to make one findViewById(v.getId()), just cast a view that you receive as a parameter, EditText et = (EditText)v

-1

You can have your class implement the Onfocuschangelistener, thus:

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

    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.