EditText setOnFocusChangeListener on all EditTexts(所有 EditTexts 上的 EditText setOnFocusChangeListener)
问题描述
我有几个 EditText 字段,我想用 setOnFocusChangeListener 将它们保存到 SQLiteDatabase.我是否必须为每个单独设置一个 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
.这样,您的所有更改都将在该方法上进行,但您必须通过使用 v.getId()
获取视图 ID 来检查哪个视图更改了焦点并进行相应处理.
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
}
}
这篇关于所有 EditTexts 上的 EditText setOnFocusChangeListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:所有 EditTexts 上的 EditText setOnFocusChangeListener


- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01