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

Android 11(API 30)设备文本框点击后被键盘遮挡 Android 10运行正常

这是Android 11(API 30)对输入法窗口布局规则调整导致的适配问题,核心诱因有两点:

  • Android 11起系统默认启用边到边显示逻辑,不再自动为应用调整窗口大小避让输入法,需要输入法侧主动上报自身占用的高度,旧版本的自动高度计算逻辑在API 30+上失效。
  • 你当前代码中在onCreateInputView()内主动调用onCreateCandidatesView()不符合输入法生命周期规范,候选栏创建时机错误会导致系统计算输入法总高度时遗漏候选栏高度,最终返回给上层应用的避让高度偏小,出现文本框被遮挡的问题。

修复方案

  • 移除onCreateInputView()中的onCreateCandidatesView()主动调用,候选栏的创建由系统根据输入场景自动触发,手动调用会导致高度计算错乱。
  • 重写onComputeInsets()方法,主动向系统上报输入法的实际占用高度,这是API 30+必须适配的逻辑:
@Override
public void onComputeInsets(Insets outInsets) {
    super.onComputeInsets(outInsets);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        int inputHeight = kv != null ? kv.getHeight() : 0;
        int candidateHeight = wordBar != null ? wordBar.getHeight() : 0;
        int totalImeHeight = inputHeight + candidateHeight;
        outInsets.contentTopInsets = totalImeHeight;
        outInsets.visibleTopInsets = totalImeHeight;
    }
}
  • 检查输入法服务在AndroidManifest.xml中的配置,明确指定windowSoftInputMode为adjustResize:
<service
    android:name=".你的输入法服务类名"
    android:permission="android.permission.BIND_INPUT_METHOD"
    android:windowSoftInputMode="adjustResize">
    <intent-filter>
        <action android:name="android.view.InputMethod" />
    </intent-filter>
    <meta-data
        android:name="android.view.im"
        android:resource="@xml/method" />
</service>

内容的提问来源于stack exchange,提问作者Ali Tayyip Uzunoglan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 01:27:00