最近公司UI 要求在键盘输入时为了布局的美观,最好可以更改回车键为搜索和完成,并对相关的事件做处理。 经过查找资料,在EditText中有个imeOptions方法可以修改回车键显示内容。 imeOptions 参数主要有:actionDone ,actionGo ,actionNext, actionSearch ,actionSend等,具体可以查看相关的文档(https://developer.android.com/training/keyboard-input/style.html 和 https://developer.android.com/reference/android/widget/TextView.html#attr_android:imeOptions) 。里面有详细的介绍使用方法
下面以actionDone 为例子介绍相关的使用方法。 布局文件如下:
<EditText
android:id="@+id/storage_size_tv"
android:layout_width="90dp"
android:gravity="right"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="number"
android:maxLines="1"
android:background="@null" />
code:主要是对EditText设置OnEditorActionListener,监听回车键的触发做相应的操作
storage_size_tv.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == KeyEvent.KEYCODE_ENTER){//这里可以做具体的操作
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
return true;
}
return false;
}
});
具体的效果如下图所示:
ps:官方文档是个好东西,有空时候还是要多看看