我该如何从软键盘中捕获特定的按键事件? 具体来说,我对“完成”键感兴趣。
我不太确定被接受的答案使用了哪种监听器。
我使用了附加到EditText
的OnKeyListener
,但它无法捕获next和done。
然而,使用OnEditorActionListener
就可以,并且还允许我通过比较动作值与定义的常量EditorInfo.IME_ACTION_NEXT
和EditorInfo.IME_ACTION_DONE
来区分它们。
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId & EditorInfo.IME_MASK_ACTION) != 0) {
doSomething();
return true;
}
else {
return false;
}
}
});
@Swato的答案对我来说不完整(并且不能编译!),因此我将展示如何与DONE和NEXT操作进行比较。
@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来监听“enter”或“next”,而不能使用OnKeyListener。从文档中可以看到:
由于软输入法可以使用多种创新的方式输入文本,因此不能保证软键盘上的任何按键都会生成一个按键事件:这取决于输入法的自主选择,事实上不鼓励发送这样的事件。您不应该仅依靠接收软输入法上任何按键的KeyEvent。
参考资料:http://developer.android.com/reference/android/view/KeyEvent.html
注意:这个答案已经过时,不再适用。请查看下面的答案。
你可以捕获KeyEvent然后检查它的keycode。FLAG_EDITOR_ACTION被用来识别来自一个输入法的enter键,该输入法的enter键已被自动标记为“next”或“done”。
if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
//your code here
在此处查找文档。
就这样做:
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监听器不起作用,因为软件输入法中的按键通常不会触发此监听器的方法,而该回调函数在视图中按下硬件键时被调用。
// 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;
}
EditText
中都能很好地工作!谢谢:-) - maresmar<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;
}
});
Kotlin版本:
<EditText android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
// Get reference to EditText.
val editText = findViewById<EditText>(R.id.edit_text)
editText.setOnEditorActionListener { _, actionId: Int, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Do your logic here.
true
} else {
false
}
}
<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" />
OnEditorActionListener
和OnKeyListener
两种方式对我都有效。 - code_dredd