整理自:http://hi.baidu.com/doyee/blog/item/e2a8481628ebed4521a4e948.html
按下"
Done
"按钮的默认行为是关闭软键盘,但是我们可以通过
EditText
的
setOnEditorActionListener()
方法来设置
On EditorActionListener
以便添加自己的行为.
在EditText中,可以使用setImeOptions()方法来来开启软键盘的"Done"按钮。
示例代码如下:
editText.setImeOptions(EditorInfo.IME_ACT ION_DONE);
界面如下:
捕获Android文本输入框的软键盘完成(Done)按键消息:
- editText.setOnEditorActionListener(new EditText.On
EditorActionListener() { - @Override
- public boolean on
EditorAction(TextView v, int actionId, KeyEvent event) { - if (actionId == EditorInfo.IME_ACT
ION_DONE) { - InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
- doSomething();
- return true;
- }
- return false;
- }
- });
EditorInfo.IME_ACT ION_DONE
可以和其他的标志一起组合使用来设置软键盘,比如:
editText.setImeOptions(
EditorInfo.IME_FLAG_NO_EXTRACT_UI
|
EditorInfo.IME_ACT ION_DONE
);
注意1
:
EditorInfo.IME_ACT ION_DON
E
只有对
android:singleLine="true"
的EditText有效。至少对HTC_A9191是这样的。
注意2:对于
EditorInfo.IME_ACT ION_DON
E
,有些输入法并不支持它,比如搜狐拼音输入法。