重写EditText回车等事件
我们在用到EditText这个空间时经常需要重写软键盘中的回车事件以配合我们接下来的响应,比如点击回车变成搜索、发送、完成等。
EditText为我们提供了一个属性imeOptions用来替换软键盘中enter键的外观,如actionDone会使外观变成“完成”。
下面列出比较经常用到的几个属性以及替换的文本外观:
actionUnspecified EditorInfo.IME_ACTION_UNSPECIFIED 未指定
actionNone EditorInfo.IME_ACTION_NONE 动作
actionGo EditorInfo.IME_ACTION_GO 去往
actionSearch EditorInfo.IME_ACTION_SEARCH 搜索
actionSend EditorInfo.IME_ACTION_SEND 发送
actionNext EditorInfo.IME_ACTION_NEXT 下一项
actionDone EditorInfo.IME_ACTION_DONE 完成
设置的方法可以在布局文件中设置 android:imeOptions=”actionNext”
在代码中 edittext.setImeOptions(EditorInfo.IME_ACTION_NEXT);
edittext.setImeOptions(EditorInfo.IME_ACTION_UNSPECIFIED);
edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//此处会响应2次 分别为ACTION_DOWN和ACTION_UP
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER &&
event.getAction() == KeyEvent.ACTION_DOWN) {
return true;
}
return false;
}
});