android键盘的Done按钮

在EditText中,可以使用setImeOptions()方法来来开启软键盘的"Done"按钮。
示例代码如下:editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

 按下"Done"按钮的默认行为是关闭软键盘,但是我们可以通过EditTextsetOnEditorActionListener()方法来设置OnEditorActionListener以便添加自己的行为.

捕获Android文本输入框的软键盘完成(Done)按键消息:
    editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {  
      
        @Override  
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
            if (actionId == EditorInfo.IME_ACTION_DONE) {  
                InputMethodManager imm = (InputMethodManager)v.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(),
0); doSomething(); return true; } return false; } });

EditorInfo.IME_ACTION_DONE可以和其他的标志一起组合使用来设置软键盘,比如:
editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI|EditorInfo.IME_ACTION_DONE); 
注意1EditorInfo.IME_ACTION_DONE只有对android:singleLine="true"的EditText有效。至少对HTC_A9191是这样的。
注意2:对于EditorInfo.IME_ACTION_DONE,有些输入法并不支持它,比如搜狐拼音输入法。
posted @ 2013-07-01 21:19  马小豆包  阅读(2653)  评论(0编辑  收藏  举报