Android EditText通过imeOptions属性控制软键盘的回车键变化:回车,搜索,下一个,完成等,并响应事件
1.在Layout文件中,给EditText文件添加属性android:imeOptions=""
属性值含义:
actionUnspecified:未指定
actionNone :没有动作
actionSearch: 搜索
actionSend: 发送
actionNext: 下一项
actionGo:去往
actionDone :完成
对应用到的常量:
EditorInfo.IME_ACTION_UNSPECIFIED
EditorInfo.IME_ACTION_NONE
EditorInfo.IME_ACTION_SEARCH
EditorInfo.IME_ACTION_SEND
EditorInfo.IME_ACTION_NEXT
EditorInfo.IME_ACTION_GO
EditorInfo.IME_ACTION_DONE
2.EditText添加监听器
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId==EditorInfo.IME_ACTION_SEND ||(event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER))
//do something;
{
return true;
}
return false;
}
});
注意事项:
1.版本高于2.3,EditText设置inputType为text或singleLine为true才会有效果
2.onEditorAction需返回true,不然方法会执行两次
更多相关文章
- FrameLayout的使用——android开发之xml布局文件
- android中将数据写入手机内存和sdcard中的文件
- android:installLocation这个属性
- android+spring boot 选择,上传,下载文件
- Eclipse下Android工程无法自动产生R文件解决