Android 开发问题:EditText 控件的 android:imeOptions=“actionDone“ 属性不生效

xml 复制代码
<EditText
    android:id="@+id/et_test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入内容"
    android:imeOptions="actionDone" />
  • 在 Android 开发中,上述代码中,EditText 控件的 android:imeOptions="actionDone" 属性不生效,即无法实现点击软键盘上的"完成"按钮时
问题原因
  1. android:imeOptions="actionDone" 属性用于将软键盘右下角的回车按钮变为"完成"按钮,目的是为了隐藏软键盘

  2. 根本原因是未设置单行输入,系统优先允许换行,但也可以监听此动作执行自定义操作

处理方法
  1. 使用 android:inputType="text" 属性,设置为单行输入
xml 复制代码
<EditText
    android:id="@+id/et_test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入内容"
    android:imeOptions="actionDone"
    android:inputType="text" />
  1. 或者,使用监听器监听此动作执行自定义操作
xml 复制代码
<EditText
    android:id="@+id/et_test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入内容"
    android:imeOptions="actionDone" />
java 复制代码
EditText etTest = findViewById(R.id.et_test);

etTest.setOnEditorActionListener((v, actionId, event) -> {

    Log.i(TAG, "actionId: " + actionId);

    if (actionId == EditorInfo.IME_ACTION_DONE) {

        // 隐藏软键盘
        hideKeyboard();

        // 清除焦点
        v.clearFocus();

        // 表示监听器已经处理这个事件,系统不需要再处理
        return true;
    }

    // 针对某些输入法的情况
    if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
        if (event != null && event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
            hideKeyboard();
            v.clearFocus();
            return true;
        }
    }

    return false;
});
java 复制代码
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
相关推荐
霸道流氓气质几秒前
Spring AI 技术细节:VectorStore 多库统一抽象
java·人工智能·spring
今天AI了吗1 分钟前
Codex 配置自定义 AI API 完整指南:从零到一接入你的专属模型
java·人工智能·python·数据分析·embedding
学长毕业设计21 分钟前
基于SpringBoot的健康食谱管理系统的设计与实现(源码+文档+讲解视频)
java·spring boot·后端
珍珠先生22 分钟前
P10 · 缺 MySQL 驱动 jar:ClassNotFoundException: com.mysql.cj.jdbc.Driver
java
Crazy_MT43 分钟前
Android Studio 运行 Flutter iOS 真机白屏,但 Xcode 和命令行正常的排查记录
flutter·android studio
我命由我1234543 分钟前
Android Jetpack Compose 开发问题:Unresolved reference ‘Icons‘.
android·java-ee·kotlin·android studio·android jetpack·android-studio·android runtime
爱笑鱼43 分钟前
Android 系统启动机制(六):Zygote 创建 App 为什么走 Socket?哪些是源码事实,哪些是架构解释?
android·linux
pnoker1 小时前
从工业软件到 AI 智能体:工业 AIoT 技术路线的系统梳理
java·人工智能·物联网·microsoft·开源·工业互联网
AI2中文网1 小时前
安卓模拟器启动卡在进度条?两步解决硬件加速冲突
android
专注仿真1 小时前
Vapor框架构建及Swift for TensorFlow图像分析
java·其他