Answers:
您可以通过在OnKeyListener
上设置来实现EditText
。
这是我自己的代码中的示例。我有一个EditText
named addCourseText
,addCourseFromTextBox
单击Enter键或d-pad时将调用该函数。
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;
}
});
android:inputType="text"
编辑文本的xml,以显示Enter键,而不显示具有回车符的默认键盘。
<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"
android:inputType="text"
以及
我们也可以使用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
}
为了避免将焦点移到下一个可编辑字段(如果有),您可能要忽略按下事件,但要处理按下事件。我也更喜欢先对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;
}
}
});
这是我如何处理我的一个应用程序的示例
//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;
}
});
实现此目的的最新方法是:
将此添加到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
}