所有EditTexts上的EditText setOnFocusChangeListener

时间:2013-05-01 01:49:28

标签: android view android-edittext

我有几个EditText字段,我想用setOnFocusChangeListener保存到SQLiteDatabase。我是否必须单独在每个上设置一个onFocusChangeListener,或者是否有某种类型的捕获? (getActivity()。findViewByID,因为这是一个片段)

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

喜欢......有没有办法让ArrayList< EditText> EditText Views,为现有的editTexts指定一个指针(对不起,不确定如何),并将onFocusChangeListener设置为整个arraylist?或者,甚至,遍历ArrayList并将onFocusChangeListener设置为每个成员?

或者是一种检测任何onFocusChangeListener事件的方法,只是将所有数据保存到数据库,无论偶然发生什么EditText?

4 个答案:

答案 0 :(得分:21)

好吧,你可以让你的活动实施OnFocusChangeListener。这样,您所做的所有更改都将在一个方法上进行,但是您必须通过使用v.getId()获取视图ID来检查哪个视图更改了焦点并进行相应处理。

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

    ...etc
    }
}

答案 1 :(得分:9)

有很多方法可以让这更简单。首先,如何将此抽象为一个方法并为每个要为其添加事件的TextView调用它:

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

然后,您可以为每个TextView实例调用此方法:

setOnFocusChangeListener(txtName, "name");
setOnFocusChangeListener(txtCompany, "company");
setOnFocusChangeListener(txtPosition, "position");

答案 2 :(得分:8)

我的建议是使用自定义EditText为您处理此问题:

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

您可能决定以不同方式处理客户端和数据密钥,但您的想法是使用自动处理它的自定义基类。

答案 3 :(得分:3)

甚至更简单

OnFocusChangeListener listener;

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

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

由于onFocusChange(View v,boolean hasFocus)具有参数View,您可以使用它来告诉哪个视图称为