布局文件:
-
<EditText
-
android:id= "@+id/et_drug_weight"
-
android:layout_width= "0dp"
-
android:layout_height= "match_parent"
-
android:layout_marginLeft= "20dp"
-
android:layout_weight= "1"
-
android:gravity= "center"
-
android:singleLine= "true"
-
android:imeOptions= "actionDone"
-
android:hint= "0"
-
android:inputType= "number"
-
android:maxLength= "4"
-
android:textSize= "14sp" />
-
imeOptions
imeOptions表示要设置的行为模式,常用的有以下几种:actionUnspecified 未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.
actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE
actionGo 去往,对应常量EditorInfo.IME_ACTION_GO
actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH
actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND
actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT
actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE
但是,不同的输入法会用不同的方式实现以上行为,比如,有的actionSearch会是“搜索”文字,有的会是一个放大镜图标,有的actionDone会是“完成”文字,有的会是一个回车图标。
imeActionLabel
二:在代码中监听按键
-
editText.setOnEditorActionListener( new TextView.OnEditorActionListener() {
-
-
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
-
if (actionId == EditorInfo.IME_ACTION_DONE) {
-
//TODO:你自己的业务逻辑
-
return true;
-
}
-
return false;
-
}
-
});
etSearch.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
//先隐藏键盘
((InputMethodManager) getActivity().getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(getActivity().getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//其次再做相应操作
inputContent = etSearch.getText().toString();
if (StringUtils.isBlank(inputContent)) {
} else {
//做相应的操作
}
}
return false;
}
});