Android 软键盘中的回车功能改为搜索功能

来源:互联网 发布:软件项目结案报告 编辑:程序博客网 时间:2024/05/17 07:19

1、在布局文件当中,找到EditText控件,设置imeOptions属性为actionSearch。如下:

<EditText
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionSearch"
        android:maxLines="1" />

2、需要给这个EditText控件设置监听器,用来监听软键盘的输入:

searchEdit.setOnKeyListener(new OnKeyListener() {

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_ENTER){//修改回车键功能
// 先隐藏键盘
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(
ClassListInSchoolActivity.this
.getCurrentFocus()
.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);

//TODO 执行想要搜索的功能代码


return true;
}
return false;
}
});

3、结束。。。

0 0
原创粉丝点击