You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Expandable ListView中EditText焦点异常问题求助

解决ExpandableListView子项EditText无法获取焦点的问题

我之前也踩过类似的坑,ExpandableListView在焦点处理上确实有点“抢戏”,结合你已经设置了adjustPan的情况,给你几个逐步排查的解决方案:

1. 先检查子项布局的焦点属性

这是最容易忽略的基础问题:

  • 确保子项的根布局(比如LinearLayout、RelativeLayout)没有设置android:focusable="true"或android:focusableInTouchMode="true"——父容器如果先把焦点抢走了,EditText自然拿不到。
  • 给你的EditText明确加上焦点相关属性,同时补充点击权限,确保它能响应触摸:
    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:clickable="true"/>
    

2. 自定义ExpandableListView,拦截焦点抢夺

原生ExpandableListView会默认拦截子项的焦点请求,我们可以自定义一个子类,让它对EditText“网开一面”:

public class CustomExpandableListView extends ExpandableListView {
    public CustomExpandableListView(Context context) {
        super(context);
    }

    public CustomExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomExpandableListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // 定位触摸的子View,判断是否为EditText
        int touchY = (int) ev.getY();
        View touchedView = getChildAt(touchY / getHeight());
        if (touchedView instanceof EditText) {
            return false; // 不拦截触摸事件,让EditText自己处理
        }
        return super.onInterceptTouchEvent(ev);
    }
}

之后在布局文件里替换成这个自定义的ListView即可。

3. 在Adapter里手动强制EditText获取焦点

如果上面的方法还不行,可以在getChildView方法里给EditText设置触摸监听,主动请求焦点:

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.your_child_item_layout, parent, false);
    }

    EditText editText = convertView.findViewById(R.id.edit_text);
    editText.setOnTouchListener((v, event) -> {
        // 强制请求焦点和触摸触发的焦点
        v.requestFocus();
        v.requestFocusFromTouch();
        return false; // 让后续触摸事件继续传递,不拦截
    });

    return convertView;
}

4. 确认windowSoftInputMode无冲突

你已经设置了adjustPan,检查一下清单文件里是不是同时设置了其他可能冲突的属性(比如adjustResize),确保属性值是android:windowSoftInputMode="adjustPan",没有多余的配置。

按这个顺序排查,一般都能解决焦点问题。

内容的提问来源于stack exchange,提问作者Hybris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:17:03