Android setOnEditorActionListener() не запускается
Я пытаюсь заставить слушателя EditText
когда будет нажата кнопка ввода. Но она вообще не сработала. Я проверял это на LG Nexus 4 с Android 4.2.2. setOnEditorActionListener
работает на Amazon Kindle Fire с Android 2.3 и setImeActionLabel
нигде не работает! Я также не могу установить текст для Enter
Кнопка. Вот код:
mEditText.setImeActionLabel("Reply", EditorInfo.IME_ACTION_UNSPECIFIED);
mEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
Log.d("TEST RESPONSE", "Action ID = " + actionId + "KeyEvent = " + event);
return true;
}
});
Что я делаю неправильно? Как я могу это исправить?
9 ответов
Вы можете использовать TextWatcher.
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.charAt(s.length() - 1) == '\n') {
Log.d("TEST RESPONSE", "Enter was pressed");
}
}
});
Убедитесь, что в файле макета установлен IME_ACTION:
<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" />
Для полного объяснения см. http://developer.android.com/guide/topics/ui/controls/text.html.
Что сработало для меня, это, я добавил эту строку ниже, чтобы EditText
android:imeOptions="actionSend"
эта строка делает клавиатуру, которая появляется при нажатии на тексте редактирования, имеет кнопку отправки вместо поиска
в setOnEditorActionListener вы переопределяете следующий метод, ищущий действие send
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
//implement stuff here
}
}
Я использую следующий код
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@id/ic_magnify"
android:layout_centerVertical="true"
android:textSize="15sp"
android:textColor="#000"
android:id="@+id/input_search"
android:inputType="text"
android:background="@null"
android:hint="Enter Address, City or Zip Code"
android:imeOptions="actionSearch"/>
mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent keyEvent) {
if(actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE
|| keyEvent.getAction() == KeyEvent.ACTION_DOWN || keyEvent.getAction() == KeyEvent.KEYCODE_ENTER){
geoLocate();
}
return false;
}
});
Я понял, это работает с Edittext в Activity, но не в Fragement.
<EditText
android:id="@+id/mEditText"
android:imeOptions="actionSearch"
android:imeActionLabel="搜索"
android:inputType="text"
android:singleLine="true"
android:textSize="18dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:textColor="@color/black"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
android:shadowColor="#4b000000"
android:shadowDy="1"
android:shadowDx="1"
android:shadowRadius="1"
android:cursorVisible="true"
android:textCursorDrawable="@null"
android:gravity="center_vertical|left"
android:background="@color/transparent"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
tools:ignore="UnusedAttribute">
<requestFocus/>
</EditText>
mEditText.setImeActionLabel("搜索", EditorInfo.IME_ACTION_SEARCH);
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
MToastUtil.show("搜索");
}
return false;
}
});
Вы можете попробовать это, чтобы использовать OnKeyListener
editText.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int actionId, KeyEvent event) {
Log.d("TEST RESPONSE", "Action ID = " + actionId + "KeyEvent = " + event);
return false;
}
});
После небольшого дополнительного поиска - это было решение для меня ( работает также фрагментом):
Просто удалите imeAction