软键盘的Enter键默认显示的是“完成”文本,我们知道按Enter建表示前置工作已经准备完毕了,要去什么什么啦。比如,在一个搜索中,我们输入要搜索的文本,然后按Enter表示要去搜索了,但是默认的Enter键显示的是“完成”文本,看着不太合适,不符合搜索的语义,如果能显示“搜索”两个字或者显示一个表示搜索的图标多好。事实证明我们的想法是合理的,Android也为我们提供的这样的功能。通过设置android: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 效果:

private
TextView.OnEditorActionListener mWriteListener =
new TextView.OnEditorActionListener()
{
public boolean
onEditorAction(TextView view, int actionId, KeyEvent event) {
// If the
action is a key-up event on the return key, send the message
if (actionId
== EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
String
message = view.getText().toString();
sendMessage(message);
}
if(D) Log.i(TAG,
"END onEditorAction");
return true;
}
};
//隐藏软键盘
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//显示软键盘,控件ID可以是EditText,TextView
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(控件ID, 0);
本文介绍了如何在Android中更改EditText软键盘的Enter键显示文本,以符合不同场景的需求,如搜索、发送等。通过设置android:imeOptions属性,并使用不同的常量值,如IME_ACTION_SEARCH,可以实现Enter键功能的定制。同时提供了隐藏和显示软键盘的方法。
624

被折叠的 条评论
为什么被折叠?



