你好我有一个搜索EditText
和搜索Button
。当我键入搜索的文本时,我想使用软键盘上的ENTER键而不是搜索Button
来激活搜索功能。
提前感谢您的帮助。
你好我有一个搜索EditText
和搜索Button
。当我键入搜索的文本时,我想使用软键盘上的ENTER键而不是搜索Button
来激活搜索功能。
提前感谢您的帮助。
你可以通过OnKeyListener
在你的EditText
.
这是我自己的代码中的一个示例。我有一个EditText
named ,它会在单击 enter 键或 d-pad 时addCourseText
调用该函数。addCourseFromTextBox
addCourseText = (EditText) findViewById(R.id.clEtAddCourse);
addCourseText.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
addCourseFromTextBox();
return true;
default:
break;
}
}
return false;
}
});
<EditText
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/search_hint"
android:inputType="text"
android:imeOptions="actionSend" />
然后,您可以通过为 EditText 元素定义 TextView.OnEditorActionListener 来监听操作按钮上的按下。在您的侦听器中,响应 EditorInfo 类中定义的相应 IME 操作 ID,例如 IME_ACTION_SEND。例如:
EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
});
来源:https ://developer.android.com/training/keyboard-input/style.html
可能你可以像这样向你的 EditText 添加一个属性:
android:imeOptions="actionSearch"
向 EditText 添加一个属性,例如 android:imeOptions="actionSearch"
这是执行该功能的最佳方法
并且 imeOptions 也有一些其他的值,如 "go" 、"next"、"done" 等。
我们也可以使用 Kotlin lambda
editText.setOnKeyListener { _, keyCode, keyEvent ->
if (keyEvent.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
Log.d("Android view component", "Enter button was pressed")
return@setOnKeyListener true
}
return@setOnKeyListener false
}
实现这一目标的最新方法是:
将此添加到 XML 中的 EditText:
android:imeOptions="actionSearch"
然后在您的活动/片段中:
EditText.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// Do what you want here
return@setOnEditorActionListener true
}
return@setOnEditorActionListener false
}
这是我的一个应用程序的示例我如何处理
//searching for the Edit Text in the view
final EditText myEditText =(EditText)view.findViewById(R.id.myEditText);
myEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
(keyCode == KeyEvent.KEYCODE_ENTER)) {
//do something
//true because you handle the event
return true;
}
return false;
}
});
为了避免焦点前进到下一个可编辑字段(如果有的话),您可能希望忽略按键事件,但处理按键事件。我也更喜欢先过滤 keyCode,假设它会稍微更有效率。顺便说一句,请记住,返回 true 意味着您已经处理了事件,因此其他侦听器不会。无论如何,这是我的版本。
ETFind.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER
|| keyCode == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
// do nothing yet
} else if (event.getAction() == KeyEvent.ACTION_UP) {
findForward();
} // is there any other option here?...
// Regardless of what we did above,
// we do not want to propagate the Enter key up
// since it was our task to handle it.
return true;
} else {
// it is not an Enter key - let others handle the event
return false;
}
}
});