Android开发 匹配软键盘回车键

本文介绍如何在Android应用中监听软键盘的回车键事件,包括设置EditText的属性及使用setOnEditorActionListener方法实现不同操作如搜索、发送等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在android开发过程中,有时候需要对EditText的软键盘进行监听。当点击软键盘回车位置按键的时候,需要实现 完成、前进、下一项、搜索、发送或其他功能。这就需要开发者对软键盘回车的点击事件进行捕捉。

1. 我们需要先在XML文件中设置EditText的 android:imeOptions=""属性,
(IME英文全称Input Method Editors,中文名称输入法编辑器)

  <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionSearch"
        android:singleLine="true"
         />

 注意: 需要设置`android:inputType="text"`或`android:singleLine="true"`,
        否则代码中的`setOnEditorActionListener`可能不起作用。
android:imeOptions=""的XML属性值对应 EditorInfo 常量值
actionSearchEditorInfo.IME_ACTION_SEARCH
actionSendEditorInfo.IME_ACTION_SEND
actionNextEditorInfo.IME_ACTION_NEXT
actionGoEditorInfo.IME_ACTION_GO
actionDoneEditorInfo.IME_ACTION_DONE
actionNoneEditorInfo.IME_ACTION_NONE
actionPreviousEditorInfo.IME_ACTION_PREVIOUS
actionUnspecifiedEditorInfo.IME_ACTION_UNSPECIFIED

2.在代码中对EditText设置setOnEditorActionListener监听事件。

 editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            /****
             * 
             * @param v 可以理解为是向上转型的EditText,可以用来操作当前的EditText
             * @param actionId 动作标识,是跟EditorInfo.IME_**这里的值对比可以判断执行了什么动作
             * @param event  跟KeyEvent.ACTION_**比较值判断它的事件
             * @return  如果不往下执行到此结束,返回true,否则为false。
             */
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                 if (actionId == EditorInfo.IME_ACTION_SEARCH) {//判断动作标识是否匹配
                      // To do something
                }
                return false;
            }
        });

上面是对软键盘回车键匹配的步骤,总共就两步,其实挺简单的。
以下是在MIUI系统中搜狗输入法小米版对Enter键的匹配样式。不同的系统,不同的输入法,可能会对Enter键的匹配不同,其他输入法请自行测试。

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

八个属性的源码+注释。

/**
     * Bits of {@link #IME_MASK_ACTION}: no specific action has been
     * associated with this editor, let the editor come up with its own if
     * it can.
     */
    public static final int IME_ACTION_UNSPECIFIED = 0x00000000;

    /**
     * Bits of {@link #IME_MASK_ACTION}: there is no available action.
     */
    public static final int IME_ACTION_NONE = 0x00000001;

    /**
     * Bits of {@link #IME_MASK_ACTION}: the action key performs a "go"
     * operation to take the user to the target of the text they typed.
     * Typically used, for example, when entering a URL.
     */
    public static final int IME_ACTION_GO = 0x00000002;

    /**
     * Bits of {@link #IME_MASK_ACTION}: the action key performs a "search"
     * operation, taking the user to the results of searching for the text
     * they have typed (in whatever context is appropriate).
     */
    public static final int IME_ACTION_SEARCH = 0x00000003;

    /**
     * Bits of {@link #IME_MASK_ACTION}: the action key performs a "send"
     * operation, delivering the text to its target.  This is typically used
     * when composing a message in IM or SMS where sending is immediate.
     */
    public static final int IME_ACTION_SEND = 0x00000004;

    /**
     * Bits of {@link #IME_MASK_ACTION}: the action key performs a "next"
     * operation, taking the user to the next field that will accept text.
     */
    public static final int IME_ACTION_NEXT = 0x00000005;

    /**
     * Bits of {@link #IME_MASK_ACTION}: the action key performs a "done"
     * operation, typically meaning there is nothing more to input and the
     * IME will be closed.
     */
    public static final int IME_ACTION_DONE = 0x00000006;

    /**
     * Bits of {@link #IME_MASK_ACTION}: like {@link #IME_ACTION_NEXT}, but
     * for moving to the previous field.  This will normally not be used to
     * specify an action (since it precludes {@link #IME_ACTION_NEXT}), but
     * can be returned to the app if it sets {@link #IME_FLAG_NAVIGATE_PREVIOUS}.
     */
    public static final int IME_ACTION_PREVIOUS = 0x00000007;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值