Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText setOnFocusChangeListener on all EditTexts

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?

like image 480
James Perih Avatar asked May 01 '13 01:05

James Perih


1 Answers

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
    }
}
like image 108
tyczj Avatar answered Oct 07 '22 04:10

tyczj