Android: как заставить клавиатуру ввести кнопку сказать "Поиск" и обрабатывать его клик?
Я не могу понять это. В некоторых приложениях есть EditText (текстовое поле), которое, когда вы касаетесь его, и оно выводит экранную клавиатуру, на клавиатуре есть кнопка "Поиск", а не клавиша ввода.
Я хочу реализовать это. Как я могу реализовать эту кнопку поиска и обнаружить нажатие кнопки "Поиск"?
Изменить: найдено, как реализовать кнопку поиска; в XML, android:imeOptions="actionSearch"
или в Java, EditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
. Но как я могу обработать пользователя нажатием этой кнопки поиска? Связано ли это с android:imeActionId
?
Ответы
Ответ 1
В макете задайте параметры метода ввода для поиска.
<EditText
android:imeOptions="actionSearch"
android:inputType="text" />
В java добавьте прослушиватель действия редактора.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();
return true;
}
return false;
}
});
Ответ 2
Скрыть клавиатуру, когда пользователь нажимает кнопку поиска. Дополнение к ответу Робби Понд
private void performSearch() {
editText.clearFocus();
InputMethodManager in = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
//...perform search
}
Ответ 3
В файле xml
поместите imeOptions="actionSearch"
и inputType="text"
, maxLines="1"
:
<EditText
android:id="@+id/search_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/search"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1" />
Ответ 4
В котлине
evLoginPassword.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
doTheLoginWork()
}
true
}
Частичный XML-код
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<EditText
android:id="@+id/evLoginUserEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:textColor="@color/black_54_percent" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<EditText
android:id="@+id/evLoginPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:inputType="textPassword"
android:imeOptions="actionDone"
android:textColor="@color/black_54_percent" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
Ответ 5
Этот ответ для TextInputEditText:
В XML файле макета задайте параметры метода ввода для требуемого типа. например сделано.
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionGo"/>
Точно так же вы можете также установить imeOptions для actionSubmit, actionSearch и т.д.
В Java добавьте слушатель действия редактора.
textInputLayout.getEditText().setOnEditorActionListener(new
TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
performYourAction();
return true;
}
return false;
}
});
Если вы используете kotlin:
textInputLayout.editText.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_GO) {
performYourAction()
}
true
}
Ответ 6
по XML:
<EditText
android:id="@+id/search_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/search"
android:imeOptions="actionSearch"
android:inputType="text" />
По Java:
editText.clearFocus();
InputMethodManager in = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);