EditText上setOnFocusChangeListener所有EditTexts [英] EditText setOnFocusChangeListener on all EditTexts
问题描述
我有我想保存与setOnFocusChangeListener的SQLiteDatabase数的EditText领域。我一定要设置单独的每一个的onFocusChangeListener,还是有一个包罗万象的某种? (getActivity()。findViewByID,因为这是一个片段)
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());
}
}
});
像......是有一些方法有一个ArrayList的<中的EditText视图的EditText>,分配一个指针(对不起,不知道如何),以现有的editTexts并设置onFocusChangeListener整个数组列表?或者,甚至,遍历ArrayList和设置onFocusChangeListener到每个成员?
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?
或者一种方法来检测任何onFocusChangeListener事件,只是所有的数据保存到数据库中,而不管EDITTEXT的甚至发生?
Or a way to detect ANY onFocusChangeListener events, and just save all data to the database, regardless of what EditText the even occurred on?
推荐答案
好,你可以有你的活动实施 OnFocusChangeListener
这样所有的修改将是一个方法,但你将不得不通过获得视图id与 v.getId()检查哪些视图中更改焦点
和相应的处理
well you could have your activity implement OnFocusChangeListener
that way all your changes will be on that one method but you will have to check which view changed focus by getting the view id with v.getId()
and handle accordingly
@Override
public void onFocusChanged(View v, boolean hasFocus) {
switch(v.getId()){
case r.id.editText1:
break;
...etc
}
}
这篇关于EditText上setOnFocusChangeListener所有EditTexts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!