【发布时间】:2011-03-03 04:37:12
【问题描述】:
如何从软键盘捕捉特定的按键事件? 特别是我对“完成”键感兴趣。
【问题讨论】:
标签: android
如何从软键盘捕捉特定的按键事件? 特别是我对“完成”键感兴趣。
【问题讨论】:
标签: android
我不太确定在接受的答案中使用了哪种侦听器。
我使用了OnKeyListener 附加到EditText,但它无法捕捉下一个也无法完成。
但是,使用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;
}
}
});
【讨论】:
OnEditorActionListener 和 OnKeyListener 两种方式都对我有用。
@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。来自文档:
由于软输入法可以使用多种创造性的文本输入方式,因此不能保证软键盘上的任何按键都会产生按键事件:这由 IME 自行决定,实际上会发送不鼓励此类事件。您永远不应该依赖在软输入法上为任何键接收 KeyEvents。
参考:http://developer.android.com/reference/android/view/KeyEvent.html
【讨论】:
注意:此答案已过时,不再有效。请参阅下面的答案。
您捕获 KeyEvent,然后检查其键码。 FLAG_EDITOR_ACTION 用于识别来自 IME 的输入键,其输入键已自动标记为“下一步”或“完成”
if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
//your code here
查找文档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 中也能正常工作!谢谢:-)
注意:在您的编辑文本中提及输入类型。
<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" />
不要忘记设置 android:inputType。
// 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,它会自动在 ListView 的下方显示结果。 SoftInput 键盘只显示“下一步”按钮并输入符号 - 这没有做任何事情。我只想要完成按钮(没有下一步或输入符号),而且我想要它在按下时,它应该关闭键盘,因为用户应该在它下面看到结果。
我在 Cyril Mottier 先生的博客上找到的解决方案/非常简单,无需任何额外代码即可工作: 在 EditText 所在的 xml 中,应该这样写: android:imeOptions="actionDone"
所以用完成按钮隐藏键盘,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" />
【讨论】:
IME_MASK_ACTION是255,而接收到的actionId是6,我的编译器不接受
if (actionId & EditorInfo.IME_MASK_ACTION)
这是一个整数。无论如何,&-ing 255 有什么用?所以测试就可以了
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE)
...
【讨论】: