android listview edittext 焦点问题,Listview 解决edittext焦点的简易方案

本文介绍了在Android中ListView中遇到的EditText焦点问题及内容错乱的解决方案。通过设置ListView的descendantFocusability属性为beforeDescendants,或者在item布局中设置EditText的descendantFocusability为blocksDescendants,可以解决焦点问题。对于内容错乱,通过监听EditText的TextWatcher,更新对应数据源来避免。同时,建议使用RecyclerView代替ListView以避免这类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一般都会出现的问题:

1.在listview中editetext焦点的问题 .

2.Listview复用机制导致editext内容错乱问题.

一、在listview中editetext焦点的问题?

需要在xml布局中给listview设置一个descendantFocusability属性,这个 属性一共有三个值beforeDescendants、afterDescendants、blocksDescendants。

beforeDescendants:viewgroup会优先其子类控件而获取到焦点

afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点

blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

在这里我给listview设置descendantFocusability属性为beforeDescendants,当然也可以在itemview布局的xml中为其editetext设置descendantFocusability属性为blocksDescendants,两种方式效果都一样。

android:id="@+id/lv_person_info"

android:descendantFocusability="beforeDescendants"

android:divider="@null"

android:cacheColorHint="@color/transparent"

android:layout_width="match_parent"

android:layout_height="wrap_content">

二、Listview复用机制导致editext内容错乱问题?

复用问题 处理也比较简单,就是给每个item设置游标,当内容改变时改变对应的数据源,为空时就清除editText的内容,直接看代码好了;

public class MatchEnrollPersonCustomInfoAdapter extends BaseAdapter{

private Context context;

private List datas;

private boolean onlylook = false;

public MatchEnrollPersonCustomInfoAdapter(Context context, List datas,boolean onlylook) {

this.context = context;

this.datas = datas;

this.onlylook = onlylook;

}

@Override

public int getCount() {

return datas.size();

}

@Override

public Object getItem(int position) {

return datas.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(final int position, View convertView, ViewGroup parent) {

ViewHodler hodler;

if(convertView == null){

hodler = new ViewHodler();

convertView = LayoutInflater.from(context).inflate(R.layout.adapter_person_custom_info_item,parent,false);

hodler.tvTitle = (TextView) convertView.findViewById(R.id.tv_person_info_title);

hodler.edContent = (EditText) convertView.findViewById(R.id.ed_person_info_content);

hodler.edContent.addTextChangedListener(new MyTextWatcher(hodler.edContent));

convertView.setTag(hodler);

}else {

hodler = (ViewHodler) convertView.getTag();

}

// 业务需求,是否可操作 输入框

if(onlylook){

hodler.edContent.setEnabled(false);

}else {

hodler.edContent.setEnabled(true);

}

hodler.edContent.setTag(position);

// TODO: 2018/11/9 设置数据源

hodler.tvTitle.setText(datas.get(position).remark);

hodler.edContent.setHint(datas.get(position).input_value+"");

if (!TextUtils.isEmpty(datas.get(position).value)) {//不为空的时候 赋值给对应的edittext

hodler.edContent.setText(datas.get(position).value);

hodler.edContent.setSelection(datas.get(position).value.length());

} else {//置空

hodler.edContent.getEditableText().clear();

}

return convertView;

}

class ViewHodler{

TextView tvTitle;

EditText edContent;

}

class MyTextWatcher implements TextWatcher{

EditText editText;

public MyTextWatcher(EditText editText){

this.editText = editText;

}

@Override

public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override

public void onTextChanged(CharSequence s, int start, int before, int count) {

int mTouchItemPosition = (int) editText.getTag();

MatchEnrollPersonDefine bean = datas.get(mTouchItemPosition);

bean.value = s.toString();

}

@Override

public void afterTextChanged(Editable s) {

}

}

}

以上感觉是解决此类问题比较简便的方式了,还有的大佬通过设置OnTouch事件,手动设置焦点,感觉还是比较繁琐的。

Recyclerview 是不会出现上述问题的,如果不是什么特殊需求非得用listview的话,建议还是使用新的控件吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值