2013-05-23 16 views
9

Próbuję ustawić detektora na EditText po naciśnięciu przycisku "Enter", ale nie uruchomił się wcale. Testowałem to na LG Nexus 4 z Androidem 4.2.2. setOnEditorActionListener działa na Amazon Kindle Fire z systemem Android 2.3 i setImeActionLabel działa nigdzie! Ja też nie można ustawić tekst na Enter button.Here jest kod:Android setOnEditorActionListener() nie uruchamia się

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; 
     } 
    }); 

Co robię źle? Jak mogę to naprawić?

Odpowiedz

7

Można użyć 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"); 
      } 
     } 
    }); 
+0

Dzięki. Działa. Ale nie wiesz dlaczego .setImeActionLabel i setOnEditorActionListener nie działa na Nexusie 4, ale działa na innych urządzeniach? – MainstreamDeveloper00

+2

Magia fragmentacji Androida Na mój Galaxy Nexus one też nie działają –

+1

Dla mnie, imeOptions działało tylko wtedy, gdy ustawiłeś singleLine = "true" w EditText. Dziwne i denerwujące, ale może to wyglądało, ale to właśnie znalazłem. – speedynomads

0

Można spróbować użyć 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; 
     } 
    }); 
+0

Nadal praca sama ... Nic – MainstreamDeveloper00

+0

mam to w moim LogCat: Akcja id = 66KeyEvent = Klucz Event {action = ACTION_UP, keyCode = KEYCODE_ENTER, scanCode = 0, metaState = 0, flagi = 0x6, repeatCount = 0, eventTime = 653776952, downTime = 653776952, deviceId = -1, source = 0x0} i używam mojego kodu – vmathieu

+1

Przeczytaj dokumenty dla View.OnKeyListener, "Jest to przydatne tylko w przypadku klawiatur sprzętowych; metoda wprowadzania oprogramowania nie ma obowiązku wywoływania tego słuchacza. "http://developer.android.com/reference/android/view/View.OnKeyListener.html – speedynomads

7

Upewnij się, że masz IME_ACTION ustawić w pliku układ:

<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" /> 

dla pełnego wyjaśnienia, patrz http://developer.android.com/guide/topics/ui/controls/text.html.

+0

A ta linia: android: inputType = "text" –

0

Mam to, działa na Edittext w działaniu, ale nie w Frief.

<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; 
     } 
    }); 
0

upewnić:

InputType = "tekst";

imeOptions = "actionSend";

0

Co pracował dla mnie jest to, dodałem to poniżej linii EditText

android:imeOptions="actionSend" 

linia ta sprawia, klawiaturę, która pojawia się po kliknięciu na edycję tekstu ma przycisku wysłać zamiast poszukiwania

w setOnEditorActionListener zastąpić następującą metodę szuka działania wysłać

@Override 
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) { 
     if (actionId == EditorInfo.IME_ACTION_SEND) { 
     //implement stuff here 
      } 
     } 
Powiązane problemy