- API接口:
EditText.setOnEditorActionListener - API用途:
给文本编辑框设置监听事件,不过这个事件,不是点击事件,也不是编辑事件,而是在编辑完成时点击软件盘的回车(当然,是否为回车按钮取决于android:imeOptions属性的设置)按钮才会触发
。 - 接口实现
需要重写TextView.OnEditorActionListener接口onEditorAction
onEditorAction参数说明
1、textView
编辑框输入的内容,比如点击了换行也会执行换行后的输出内容
2、id
事件标识,其值与android:imeOptions
属性设置的参数有关,与EditorInfo.IME_*
做匹配
3、keyEvent
触发事件,与KeyEvent.ACTION_*
做匹配(备注:keyEvent对象有可能会为null,原因待研究)
4、返回值 true表示不关闭软键盘,false表示关闭软键盘 - 代码演示:
EditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
if (id == EditorInfo.IME_NULL) {
return true;
}
return false;
}
});