可以通过设置EditText的 imeOptions属性,来改变'回车'键的图标1.actionUnspecified 未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED. 2.actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE3.actionGo 去往,对应常量EditorInfo.IME_ACTION_GO 4.actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH5.actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND6.actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT7.actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE
方法/步骤
1
有两种方式可以获取到这个事件第一种方法:EditText设置onEditorActionListener监听 @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId==EditorInfo.IME_ACTION_NEXT) {//EditorInfo.IME_ACTION_NEXT要和EditText 的imeOptions属性一样//处理业务逻辑 return true;//返回true,消费这个事件 } return false; }
2
第二种方法:EditText设置onKeyListener监听@Override public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_ENTER&&event.getAction()==KeyEvent.ACTION_DOWN){//业务逻辑.... return true; } return false; }
3
这里我比较推荐第一种方式,第二种方式容易和厂商的回车键产生冲突,当点击确定键时,可能会跳到下一个输入框,或者直接执行逻辑了。部分电视机遥控使用KEYCODE_ENTER(回车),也有使用KEYCODE_DPAD_CENTER(导航键 确认键)