[英]Android setOnEditorActionListener() doesn't fire
當按下輸入按鈕時,我試圖將偵聽器設置為EditText
。但它根本沒有觸發。 我在使用 Android 4.2.2 的 LG Nexus 4 上對此進行了測試。 setOnEditorActionListener
適用於 Android 2.3 的 Amazon Kindle Fire,而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;
}
});
我究竟做錯了什么? 我怎樣才能解決這個問題?
您可以使用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("TAG", "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 中,您覆蓋以下方法尋找動作發送
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
//implement stuff here
}
}
在 xml 文件中添加標簽android:inputType="text"
到 EditText
我使用以下代碼
<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;
}
});
您可以嘗試使用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;
}
});
我明白了,它適用於活動中的 Edittext,但不適用於 Fragment。
<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;
}
});
確保:
inputType = "文本";
imeOptions = "actionSend";
經過一些更多的搜索 - 這對我來說是解決方案(也適用於片段):
只需刪除imeAction
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.