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);
相关推荐
Flynt22 分钟前
Spring AI Alibaba 真停更了?我把它的 GitHub 翻了个底朝天
java·spring·开源
三少爷的鞋1 小时前
为什么成熟 Android 项目几乎看不到 IdleHandler?
android
kururunga2 小时前
Android知识点回顾之蓝牙协议
android·蓝牙·车载开发
StarsGreen3 小时前
aosp中的SELinux 策略配置和编译流程
android
她的男孩4 小时前
Spring Boot 接 Flowable:请假单要同时走财务和行政,包容分支和默认分支怎么配
java·spring boot·工作流引擎
咖啡八杯4 小时前
LoginUser 适配器模式:适配 SysUser 到 UserDetails
java·开发语言·后端·框架·适配器模式·若依
北冥有鱼5 小时前
从 bean::method 里抠出注解:Java 方法引用元数据反查全记录
java·后端
CoderYanger6 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第四弹):@ControllerAdvice 源码分析+案例代码
java·数据库·spring boot·后端·spring·java-ee·mybatis
方白羽6 小时前
Android 开发中常见的编译加速方案
android·app·gradle
StarsGreen9 小时前
Android VINTF 中 Device Manifest 与 Framework Manifest 详解
android