EditText input method action does not work when setting imeActionLabel

I have an Edittext with imeoptions as an actiongo . and I triggered my event by pressing the soft keyboard enter button.

 mModelId.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean handled = false; // if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { if (actionId == EditorInfo.IME_ACTION_GO) { id = mModelId.getText().toString(); System.out.println("Model id in Edittext:-"+ id); Toast.makeText(getActivity(), "You entered "+id, Toast.LENGTH_LONG).show(); System.out.println("Before Call Volley"); callVolley(); handled = true; } return handled; } }); 

Everything works fine, but when I add actionlabel to enter the key, the event does not fire. mModelId.setImeActionLabel("Search Model", KeyEvent.KEYCODE_ENTER); . What could be the problem?

+5
source share
4 answers

try it

declare edittext and OnEditorActionListener () like this

 mModelId = (EditText) findViewById(R.id.edittext_id); mModelId.setImeActionLabel("Search Model", KeyEvent.KEYCODE_ENTER); mModelId.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean handled = false; if (actionId == KeyEvent.KEYCODE_ENTER) { id = mModelId.getText().toString(); Toast.makeText(getActivity(), "You entered "+id, Toast.LENGTH_LONG).show(); callVolley(); handled = true; } return handled; } }); 

and you use imeoptions as actionGo, then restart it, I think it overrides ImeActionLabel try this and answer

+8
source

setImeActionLabel take two parameters, and the second int parameter should be one of those that are in the EditorInfo class. For instance:

  EditorInfo.IME_ACTION_GO EditorInfo.IME_ACTION_DONE EditorInfo.IME_ACTION_NEXT .... 

You cannot send any other integer there, for example KeyEvent.KEYCODE_ENTER

And you must set the XML as a parameter imeOptions , and singleLine , to make it work. Example:

 <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:imeOptions="actionGo" android:singleLine="true"/> 

Here is the code I used and it works:

XML layout:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true"/> </LinearLayout> 

And the base Activity code:

  mEditText2.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean handled = false; if (actionId == EditorInfo.IME_ACTION_GO) { Toast.makeText(MainActivity.this, "You entered " + v.getText().toString(), Toast.LENGTH_LONG).show(); handled = true; } return handled; } }); mEditText2.setImeActionLabel("Search Model", EditorInfo.IME_ACTION_GO); 
+2
source

I checked the versions of Android 2.1 and Android 4.0, and your code is working fine. The IME_ACTION_GO event IME_ACTION_GO reported if EditText has a singleLine parameter specified for true . If it is set to false actionId , the value IME_NULL was called regardless of setImeActionLabel or not.

In the TextView.onKeyDown method, I found that IME_NULL actionId is used when KEYCODE_ENTER detected

 mEditor.mInputContentType.onEditorActionListener.onEditorAction( this, EditorInfo.IME_NULL, event)) 

Perhaps this is a keyboard issue. Do you use any? If yes, try these changes:

instead

 mModelId.setImeActionLabel("Search Model", KeyEvent.KEYCODE_ENTER); 

it should be

 mModelId.setImeActionLabel("Search Model", EditorInfo.IME_ACTION_GO); 
+2
source
  • Set the value for the EditorInfo.actionId parameter used when connecting the input method to the text view.

    numberEditor.mInputContentType.onEditorActionListener.onEditorAction (this, EditorInfo.IME_NULL, event))

  • Set the value for the EditorInfo.actionLabel parameter used when connecting the input method to the text view.

Must be a string value using '\;' to delete characters such as "\ n" or "\ uxxxx" for a unicode character.

+2
source

Source: https://habr.com/ru/post/1204422/


All Articles