在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 常量值 |
---|---|
actionSearch | EditorInfo.IME_ACTION_SEARCH |
actionSend | EditorInfo.IME_ACTION_SEND |
actionNext | EditorInfo.IME_ACTION_NEXT |
actionGo | EditorInfo.IME_ACTION_GO |
actionDone | EditorInfo.IME_ACTION_DONE |
actionNone | EditorInfo.IME_ACTION_NONE |
actionPrevious | EditorInfo.IME_ACTION_PREVIOUS |
actionUnspecified | EditorInfo.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;