如何从软键盘上按下“完成”键

如何从软键盘捕捉特定的按键事件? 特别是我对“完成”键感兴趣。

注意:这个答案是旧的,不再有效。 请参阅下面的答案。

你赶上KeyEvent,然后检查它的键码。 FLAG_EDITOR_ACTION用于标识来自IME的input键,其input键已经被自动标记为“下一个”或“完成”

if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION) //your code here 

在这里find文档。

我不太确定接受的答案中使用了哪种听众。 我使用了附加到EditTextOnKeyListener ,它无法捕捉下一个也没有完成。

但是,使用OnEditorActionListener工作,它也允许我通过比较操作值与定义的常量EditorInfo.IME_ACTION_NEXTEditorInfo.IME_ACTION_DONE来区分它们。

 editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_MASK_ACTION) { doSomething(); return true; } else { return false; } } }); 

@ Swato的回答对我来说并不完整(也不会编译!),所以我正在展示如何对DONE和NEXT操作进行比较。

 editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { int result = actionId & EditorInfo.IME_MASK_ACTION; switch(result) { case EditorInfo.IME_ACTION_DONE: // done stuff break; case EditorInfo.IME_ACTION_NEXT: // next stuff break; } } }); 

另外我想指出,对于JellyBean和更高的OnEditorActionListener是需要听取'进入'或'下一个',你不能使用OnKeyListener。 从文档:

由于软input方法可以使用多种创造性的方式input文本,因此不能保证在软键盘上的任何按键都会产生关键事件:这由IME自行决定, 事实上发送这种事件是不鼓励的 。 您绝不应该依赖于在软input方法上接收KeyEvent。

参考: http : //developer.android.com/reference/android/view/KeyEvent.html

只是这样做:

 editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { if(actionId == EditorInfo.IME_ACTION_DONE) { //Do Something } return false; } }); 
  etSearchFriends = (EditText) findViewById(R.id.etSearchConn); etSearchFriends.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // If the event is a key-down event on the "enter" button if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { Toast.makeText(ACTIVITY_NAME.this, etSearchFriends.getText(),Toast.LENGTH_SHORT).show(); return true; } return false; } }); 

要捕获“完成”键,请从软键盘按下,重写Activity的onKeyUp方法。 为视图设置OnKeyListener侦听器将不起作用,因为软件input方法中的按键通常不会触发此侦听器的方法,因此在视图中按下硬件按键时会调用此callback。

 // Called when a key was released and not handled by any of the views inside of the activity. @Override public boolean onKeyUp(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_ENTER: // code here break; default: return super.onKeyUp(keyCode, event); } return true; } 

我有EditTextsearch名称,并自动显示下面的ListView中的结果。 SoftInput键盘只显示“下一步”button并input符号 – 哪些没有做任何事情。 我只想要完成button(没有下一个或input符号),我也想要它时,它应该closures键盘,因为用户应该看到下面的结果。

我发现/由Cyril Mottier先生在他的博客上find的解决scheme非常简单,它没有任何额外的代码:在EditText所在的xml中,这应该写成:android:imeOptions =“actionDone”

所以用Donebutton隐藏键盘,EditText应该看起来像这样:

 <EditText android:id="@+id/editText1central" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/imageView1" android:layout_toLeftOf="@+id/imageView2tie" android:ems="10" android:imeOptions="actionDone" android:hint="@string/trazi" android:inputType="textPersonName" /> 

注意:在你的编辑文本中提供inputtype。

 <EditText android:id="@+id/select_category" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" > edittext.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_DONE) { //do something here. return true; } return false; } }); 

您可以通过此方法覆盖完成的关键事件:

 editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { // do your stuff here } return false; } }); 

IME_MASK_ACTION是255,而收到actionId是6,我的编译器不接受

 if (actionId & EditorInfo.IME_MASK_ACTION) 

这是一个int。 反正255有什么用? 所以testing只是可以的

 public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) ... 
 editText = (EditText) findViewById(R.id.edit_text); editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { // code here } return false; } });