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

Android开发:点击EditText后Button无响应问题求助

问题原因与解决方案

问题根源

ScrollView设置了固定宽高(409dp/585dp),配合app:layout_constraintVertical_bias="1.0"的约束,会让它始终贴紧父布局底部。当点击EditText弹出软键盘后,布局重新计算,ScrollView会向上延伸,直接覆盖在Button上方,拦截了Button的点击事件,导致Button失去响应。移除ScrollView后无遮挡,所以功能正常。

解决方案

方案1:修正ScrollView布局约束(推荐)

将ScrollView的固定宽高改为自适应,通过约束控制其尺寸,避免覆盖Button:

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="0dp" <!-- 让约束自动分配剩余高度 -->
    android:layout_marginStart="1dp"
    android:layout_marginTop="20dp"
    android:layout_marginEnd="1dp"
    android:layout_marginBottom="2dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/startButton"
    app:layout_constraintVertical_bias="0.0"> <!-- 让ScrollView紧贴Button底部 -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/logView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</ScrollView>

方案2:提升Button层级

若需保留ScrollView固定尺寸,给Button添加elevation属性,让它始终处于ScrollView上方:

<Button
    android:id="@+id/startButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="160dp"
    android:layout_marginTop="30dp"
    android:layout_marginEnd="160dp"
    android:text="Start"
    android:elevation="4dp" <!-- 提升层级,避免被遮挡 -->
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textField" />

验证

修改后,点击EditText弹出软键盘,再点击Button即可正常输出Button pressed日志。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 08:37:02