Привет всем,
У меня проблема с AppCompatEditText
. Я переопределил AppCompatEditText
и добавил кнопку Enter
на клавиатуре (setImeOptions(EditorInfo.IME_ACTION_GO)
). Это работает нормально на всех версиях Android, ожидайте на 9.0. Вот код моего класса
public class EditTextWithGOButton extends AppCompatEditText {
private EditTextAction editTextAction;
public interface EditTextAction {
void onGoButtonClicked(String text);
}
public void setEditTextAction(EditTextAction editTextAction) {
this.editTextAction = editTextAction;
}
public EditTextWithGOButton(Context context) {
super(context);
changeImeOption();
}
public EditTextWithGOButton(Context context, AttributeSet attrs) {
super(context, attrs);
changeImeOption();
}
public EditTextWithGOButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
changeImeOption();
}
private void changeImeOption() {
this.setImeOptions(EditorInfo.IME_ACTION_GO);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
if (editTextAction != null){
editTextAction.onGoButtonClicked(getText().toString());
}
}
// Returning false allows other listeners to react to the press.
return false;
// Handle all other keys in the default way
// return super.onKeyDown(keyCode, event);
}
}
И мой XML.
<com.app.common.views.EditTextWithGOButton
android:id="@+id/edt_search_bar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.2"
android:background="@drawable/search_edittext_rounded_corner_background"
android:drawableRight="@android:drawable/ic_menu_search"
android:drawableTint="@color/colorPrimary"
android:hint="@string/search_your_activity"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:maxLines="1"
android:paddingTop="05dp"
android:paddingBottom="05dp"
android:textColorHint="@color/colorPrimary" />
Мне надоело настраивать фокусировку вручную. Когда я удаляю слушателей this.setImeOptions(EditorInfo.IME_ACTION_GO);
и onKeyDown
, AppCompatEditText
стал редактируемым, но клавиатура не выскочила. Я тоже попробовал EditText
. Но проблема осталась прежней. Любое решение ??