18

I have several EditText fields which I want to save to the SQLiteDatabase with setOnFocusChangeListener. Do I have to set an onFocusChangeListener on each one individually, or is there a catch-all of some sort? (getActivity().findViewByID because this is a fragment)

final TextView txtName = (TextView)getActivity().findViewById(R.id.clientHeader);
final TextView txtCompany = (TextView)getActivity().findViewById(R.id.txtContactCompany);       
final TextView txtPosition = (TextView)getActivity().findViewById(R.id.txtContactPosition);     


txtName.setOnFocusChangeListener(new OnFocusChangeListener() {          
    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus) {
            saveThisItem(txtClientID.getText().toString(), "name", txtName.getText().toString());
        }
    }
});


txtCompany.setOnFocusChangeListener(new OnFocusChangeListener() {          
    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus) {
            saveThisItem(txtClientID.getText().toString(), "company", txtCompany.getText().toString());
        }
    }
});

txtPosition.setOnFocusChangeListener(new OnFocusChangeListener() {          
    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus) {
            saveThisItem(txtClientID.getText().toString(), "position", txtPosition.getText().toString());
        }
    }
});

Like... is there some way to have a ArrayList < EditText > of EditText Views, assign a pointer (sorry, not sure how) to the existing editTexts and set the onFocusChangeListener to the whole arraylist? Or, even, iterate through the ArrayList and set the onFocusChangeListener to each member?

Or a way to detect ANY onFocusChangeListener events, and just save all data to the database, regardless of what EditText the even occurred on?

4 Answers 4

31

Well, you could have your activity implement OnFocusChangeListener. That way, all your changes will be on that one metho,d but you will have to check which view changed focus by getting the view id with v.getId() and handle accordingly.

@Override
public void onFocusChange(View v, boolean hasFocus) {
    switch(v.getId()){
    case r.id.editText1:
    break;

    ...etc
    }
}
3
  • This is actually the method I'm going to use; and thankfully, fragments can implement OnFocusChangedListener too! Ultimately, less code. May 1, 2013 at 2:22
  • 1
    Isn't it OnFocusChangeListener instead of OnFocusChange-d-Listener? (Hyphens added for clarity.) Obviously not a big deal, but it still took me a minute to figure out.
    – Kirk
    Sep 6, 2013 at 16:59
  • i am struggling with this, setOnFocusChangeListener is passed an anonymous inner class, how should i change that part in order to get the whole thing work ?
    – kuldeep
    Jun 3, 2015 at 13:02
13

There's plenty of ways you could make this simpler. For one, how about abstracting this into a method and calling it for each TextView you want to add the event for:

private void setOnFocusChangeListener(TextView textView, String name){

    textView.setOnFocusChangeListener(new OnFocusChangeListener() {          
        public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus) {
                saveThisItem(txtClientID.getText(), name, 
                             textView.getText());
            }
        }
    });
}

Then you can call this method for each of your TextView instances:

setOnFocusChangeListener(txtName, "name");
setOnFocusChangeListener(txtCompany, "company");
setOnFocusChangeListener(txtPosition, "position");
1
  • This is interesting. I will try. Thanks! May 1, 2013 at 2:23
8

My suggestion would be to use a custom EditText to handle this for you:

public class SaveEditText extends EditText implements View.OnFocusChangeListener {
    private String mDataKey;
    private String mClient;

    public SaveEditText (Context context) {
        super(context);
        setOnFocusChangeListener(this);
    }

    public SaveEditText (Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnFocusChangeListener(this);
    }

    public void setDataKey (String dataKey) {
        mDataKey = dataKey;
    }

    public void setClient (String client) {
        mClient = client;
    }

    @Override
    public void onFocusChange (View v, boolean hasFocus) {
        saveThisItem(mClient, mDataKey, getText().toString());
    }
}

You might decide to handle the client and data keys differently, but the idea is that you use a custom base class that will handle it automatically.

1
  • 1
    Wow, over my pay rate. Maybe I'll understand what most of that is eventually. I'm just a new guy. May 1, 2013 at 2:23
2

Or even simpler

OnFocusChangeListener listener;

listener = new OnFocusChangeListener() {          
    public void onFocusChange(View v, boolean hasFocus) {
        //Do something
    }
};

textbox1. setOnFocusChangeListener(listener);
textbox2. setOnFocusChangeListener(listener);
textbox3. setOnFocusChangeListener(listener);

Since onFocusChange(View v, boolean hasFocus) has the parameter View you can use that to tell which view called it

1
  • This works, but there are some typos in the code. Working code would be: OnFocusChangeListener listener; listener = new OnFocusChangeListener() { public void onFocusChange(View v, boolean hasFocus) { //Do something } };
    – Zerato
    Feb 20, 2018 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.