Android中处理软键盘上“回车键”的2种方法

来源:互联网 发布:nginx跳转 域名不变 编辑:程序博客网 时间:2024/06/05 09:23

1. 给EditText设置属性【android:imeOptions="actionDone"】

2.给EditText设置属性【android:inputType="text"


  
[html] view plaincopyprint?
  1. <EditText  
  2.         android:id="@+id/editText1"  
  3.         android:layout_width="match_parent"  
  4.         android:layout_height="wrap_content"  
  5.         android:singleLine="true"  
  6.         android:ems="10"  
  7.         android:inputType="text"  
  8.         android:imeOptions="actionDone">  
  9.   
  10.         <requestFocus />  
  11.     </EditText>  



2. 自己处理editText1的OnKeyListener事件。
在事件处理里面
[java] view plaincopyprint?
  1. InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);  
  2. imm.hideSoftInputFromWindow(XXX.getWindowToken(), 0);  



XXX: 应该为一个View。

下面的PS是我转载的(留着以后验证,毕竟时间还是有限的):
PS:

把EditText的Ime Options属性设置成不同的值,Enter键上可以显示不同的文字或图案
actionNone : 回车键,按下后光标到下一行
actionGo : Go,
actionSearch : 一个放大镜
actionSend : Send
actionNext : Next
actionDone : Done,隐藏软键盘,即使不是最后一个文本输入框
0 0
原创粉丝点击