EditText setOnFocusChangeListener on all EditTexts

2019-01-24 07:09发布

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条回答
干净又极端
2楼-- · 2019-01-24 07:43

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");
查看更多
我命由我不由天
3楼-- · 2019-01-24 07:43

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.

查看更多
老娘就宠你
4楼-- · 2019-01-24 07:58

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
    }
}
查看更多
在下西门庆
5楼-- · 2019-01-24 08:02

Or even simpler

OnFocusChangeListener listener;

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

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

查看更多
登录 后发表回答